Django relations naming conventions
Today is a part 2 of naming conventions from a while back. Disclaimer I may have covered this in another post! Anyway onward!
Django has three types of relations (ForeignKey's, ManytoMany and OneToOne Fields). Each of these should named appropriately to ensure the code makes logical sense and it maintainable over the long haul.
ForeignKey's should be named as a singular. The reverse relation, if specified via related_name
should be a plural or have the word set
used in it (this is the defualt).
Example: A book model would have have a single author, but the reverse would by default be book_set
or could be authored_books
A ManytoMany should be named as a plural for both the forward and reverse relation. Example: A user could be in multiple groups and a groups can have multiple users, so the names of users
and groups
would be most appropriate.
Finally a OneToOne should be singular both ways. Example: A User has a single profile and the profile has a single user.