About the related_name Attribute
The related_name
attribute at foreign keys and many-to-many relations refers to the backwards relation to this object. It is being used as a manager method and as a queryset lookup.
1 2 3 4 5 6 7 8 |
|
Then
1 2 3 4 5 6 7 8 |
|
If you create an abstract model with relations to other models, make sure to use the special syntax with %(app_label)s
and %(class)s
to have unique related names:
1 2 3 4 5 6 7 8 9 10 |
|
For the Book
model, it could look like this:
1 2 |
|
Then:
1 2 3 4 5 6 7 8 |
|
The %(app_label)s
ensures that there is no clashing from different models with the same name from different apps, for example, blog posts and forum posts.
Tips and Tricks Programming Development Databases Django 5.x Django 4.2 Django 3.2
Also by me
Django Paddle Subscriptions app
For Django-based SaaS projects.
Django GDPR Cookie Consent app
For Django websites that use cookies.