What is the preferred naming convention for Django model classes?
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.
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.
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.
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.
Django models are just Python classes, so the Python naming conventions detailed in PEP-8 apply.
For example:
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With