What is the preferred naming convention for Django model classes?
-
just name it so you know what the model stands for – kender Dec 22 '08 at 10:16
4 Answers
Django models are just Python classes, so the Python naming conventions detailed in PEP-8 apply.
For example:
- Person
- Category
- 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"

- 19,928
- 10
- 56
- 60
-
-
As per docs, I believe variable name is verbose_name_plural. https://docs.djangoproject.com/en/dev/topics/db/models/#meta-options – daydreamer Jun 01 '12 at 13:49
-
As far as I know, the idea is that the class name should be singular and should use SentenceCase with no spaces. So you'd have names like:
Person
TelephoneNumber
Then the Django admin tool knows how to pluralise them. Doesn't work so nicely for names like:
Category
which gets pluralised as Categorys, but there we go...
Apart from that, just give it a name that means something to you and succinctly sums up what the class is meant to represent.
Ben

- 66,838
- 37
- 84
- 108
-
2See ghoseb's answer above about correctly pluralizing something like Categories. – Michael Warkentin Dec 22 '08 at 18:27
In most of programming languages, people prefer give a singular names to the objects/models because these models are also represented by tables in your database system. The minimalism is a good choice everytime to avoid some meaning conflicts in the future.
To exemplify; https://stackoverflow.com/a/5841297/2643226

- 109
- 9
In adition, when you need related objects for a Model of more than one word, you can use the _set attribute. Example:
class ProcessRoom(models.Model):
...
plant = models.ForeignKey("Plant", verbose_name='planta', on_delete=models.CASCADE)
Then, related objects will be:
plant = Plant.object.get(id=1)
process_rooms = plant.processroom_set.all

- 281
- 3
- 11