Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django model naming convention

What is the preferred naming convention for Django model classes?

like image 459
Imran Avatar asked Dec 22 '08 09:12

Imran


People also ask

How do you name a Django model?

Naming Your Models The model definition is a class, so always use CapWords convention (no underscores). E.g. User , Permission , ContentType , etc. For the model's attributes use snake_case. E.g. first_name , last_name , etc.

Should Django model names be plural?

It is generally recommended to use singular nouns for model naming, for example: User, Post, Article. That is, the last component of the name should be a noun, e.g.: Some New Shiny Item. It is correct to use singular numbers when one unit of a model does not contain information about several objects.

What should I name my Django project?

Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged. So, 1 and 3 are both valid, but 3 would be the recommended approach.

What are the model inheritance style in Django?

Models inheritance works the same way as normal Python class inheritance works, the only difference is, whether we want the parent models to have their own table in the database or not. When the parent model tables are not created as tables it just acts as a container for common fields and methods.


1 Answers

Django models are just Python classes, so the Python naming conventions detailed in PEP-8 apply.

For example:

  1. Person
  2. Category
  3. ZipCode

If Django fails to pluralize the class name properly when creating the corresponding table, you can easily override the pluralization by setting a custom verbose_name_plural field in an inner META class. For example:

class Story(models.Model):     ...      class Meta:         verbose_name_plural = "stories" 
like image 149
Baishampayan Ghose Avatar answered Sep 22 '22 13:09

Baishampayan Ghose