Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django table naming convention. Can I change its behavior?

When Django creates tables it give them names of form app_class. I'm in the position of retrofitting a different (but basically similar in content) database to a Django installation. My tables name are not prepended with app_.

I could recreate my database and its tables accordingly but I'd like to see if Django has the flexibility to modify how it handles table names.

That is, I have a table coi_fs - I'd like to see if I can change the Django installation such that it will refer not to app_coi_fs but simply coi_fs?

like image 303
patfla Avatar asked Dec 19 '12 00:12

patfla


1 Answers

If you already have a database I would recommend using the database introspection option. This will create the models needed to use your current database as is.

$ django-admin.py inspectdb > models.py

To answer your original question though, from the docs (https://docs.djangoproject.com/en/dev/ref/models/options/#table-names), you can use the db_table meta property.

models.py

class DjangoSite(models.Model):
    domain = models.CharField(max_length=100)
    name = models.CharField(max_length=50)
    class Meta:
        db_table = u'site'
like image 144
Victor 'Chris' Cabral Avatar answered Oct 22 '22 02:10

Victor 'Chris' Cabral