How to remove the prefix of a table for a Django m

2019-02-04 13:52发布

I create a new django app(not project) called Bussinesses, then add following class to the models.py.

class Bussinesses(models.Model):
    business_email = models.EmailField()
    password = models.CharField(max_length=20)
    contact_first_name = models.CharField(max_length=30)
    contact_last_name = models.CharField(max_length=30)

if i use Bussinesses directly, the django will access the "bussinesses_bussinesses" table in the database, It obviously does not exist.

Because the table "bussinesses" is also used by another APP, So can't rename it. I want to know how to use the Djano model without using table prefix, and i don't want to use the raw database API directly.

Thanks a lot.

标签: django model
4条回答
孤傲高冷的网名
2楼-- · 2019-02-04 14:18

you can actually set the table name manually using the meta class, more here: https://docs.djangoproject.com/en/dev/ref/models/options/

查看更多
神经病院院长
3楼-- · 2019-02-04 14:31

You can specify the table name in the model's Meta class, using the db_table property.

Also, if you're using a database table that already exists, you might also want to take a look at the managed property. After setting a model to unmanaged syncdb will not affect it (if you reset your app, for instance), but you can still use Django's ORM normally.

查看更多
劳资没心,怎么记你
4楼-- · 2019-02-04 14:33

As an aside, if you want to change (or remove) the prefix for all tables you can provide your own meta-class. This is useful if you have multiple cases like Bussinesses where you'd just like the class and table name to be the same.

For instance the following code prefixes all tables with FOO_ instead of appname_:

class MyModelBase( ModelBase ):
    def __new__( cls, name, bases, attrs, **kwargs ):
        if name != "MyModel":
            class MetaB:
                db_table = "FOO_" + name


            attrs["Meta"] = MetaB

        r = super().__new__( cls, name, bases, attrs, **kwargs )
        return r       


class MyModel( Model, metaclass = MyModelBase ):
    class Meta:
        abstract = True


class Bussinesses( MyModel ):
    ...
查看更多
霸刀☆藐视天下
5楼-- · 2019-02-04 14:41

Just use the model meta options.

class Bussinesses(models.Model):
    business_email = models.EmailField()
    password = models.CharField(max_length=20)
    contact_first_name = models.CharField(max_length=30)
    contact_last_name = models.CharField(max_length=30)

    class Meta:
        db_table = "bussinesses"

BTW businesses is misspelled. Since you're specifying the name of the table you don't have to give your model the same name as the table, so if the table name is misspelled and you can't easily fix it, you can at least change the name of your class to the proper spelling of businesses. I would also get rid of the pluralization, and make it class Business. Finally it's not uncommon when using Django or Rails on an existing database to need to set a custom table name for every table.

查看更多
登录 后发表回答