What is the preferred naming convention for Django model classes?
A:
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
Ben
2008-12-22 10:32:05
See ghoseb's answer above about correctly pluralizing something like Categories.
Michael Warkentin
2008-12-22 18:27:54
+12
A:
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"
Baishampayan Ghose
2008-12-22 11:42:45