I have a legacy database and would like to know what good practices for naming tables, or tips on how to use inflections (as in Rails) for Django in the latest version.
I have a legacy database and would like to know what good practices for naming tables, or tips on how to use inflections (as in Rails) for Django in the latest version.
The "best practice" would be to let Django itself attribute the names of tables and fields, but that does not apply to your case, right? If you have a legacy database, you can maintain its structure as it stands - there is no need to make modifications (except in one case, as I will explain below).
Django has a tool called inspectdb
for facilitate the creation of your models, when you already have a database ready. You can use it to "kick-start," but in general you'll need adaptations.
Django has a tool called syncdb
(in future releases, will be made obsolete and replaced with migrate
) that takes care of the creation of the tables, their fields and indexes automatically, from the defined models. In future versions, you will also have schema migration tools. When you use a template like this, it is called managed .
If you do not want to use this feature - creating and maintaining your tables in another way - you can mark it as unmanaged . This is done through an attribute in the Meta
class of your model:
class Pessoa(models.Model):
id = models.IntegerField(primary_key=True)
primeiro_nome = models.CharField(max_length=70)
class Meta:
managed = False
db_table = 'nome_da_tabela'
Note that the fact that the table already exists does not automatically mean that you need an unmanaged model - you can get the table the way it is, and let Django help you with future schema migrations. This is your choice.
If you do not specify anything, Django will look for / create tables and columns with a standard name inferred from the name of your app and the names of your templates and fields. If you need to specify them manually, just use db_table
(as already shown in the example above) for the table name and / or db_column
for column names:
class Pessoa(models.Model):
id = models.IntegerField(primary_key=True, db_column="pessoa_id")
primeiro_nome = models.CharField(max_length=70, db_column="nome")
class Meta:
managed = False
db_table = 'nome_da_tabela'
There are also other options for customizing DB integration (such as db_index
and db_tablespace
), see the Django documentation for more details.
This is the only case where perhaps an adaptation in your tables is required. For Django's ORM to work, every table must have a primary key, and that primary key must be simple (ie a single column - can not be a compound key). If you do not specify anything, Django will assume that you have a numeric field called id
. If you need to specify, just use the primary_key
property, as already demonstrated in previous examples.
It took me a while to figure out what "inflections" were, but what I understood is that functionality where Rails automatically "pluralizes" the table name for you, right? There is nothing like this in Django, the default behavior is to use the singular name even for the tables (and in my opinion this is ideal - pluralizing introduces an unnecessary complication to solve a problem that does not exist).
If you really need names to be in the plural, use db_table
manually as explained above.