ORM queries are more readable and relationships more explicit when related_name
is specified.
The related_name attribute specifies the name of the reverse relation from the related model back to the model.
Django automatically creates a related_name if related_name is not set. Django uses the lower-case name of the model with the suffix _set.
If our GitHub code review bot spots this issue in your pull request it gives this advice:
1 | + | class BlogPost(models.Model): | |
2 | + | post = models.ForeignKey(User, on_delete=models.CASCADE) |
ORM queries are more readable and relationships more explicit when related_name
is specified.
3 | + | ||
4 | + | def get_user_blog_posts(user): | |
5 | + | return user.blogpost_set.all() |