ForeignKey missing related_name

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 we spot this issue in your GitHub pull request we give this advice:

models.pymaintainabilitylow
class BlogPost(models.Model):
    post = models.ForeignKey(User, on_delete=models.CASCADE)
	
def get_user_blog_posts(user):
    return user.blogpost_set.all()

ORM queries are more readable and relationships more explicit when related_name is specified.

Read more
Protect your pull requests from over 40 types of common Django technical debts with our GitHub code review bot.

Configuring this check

Django Doctor will run this check by default. No configuration is needed but the check can be turned on/off using check code missing-related-name in your pyproject.toml file.

Read more about configuring Django Doctor.