Understanding related_name in Django Models

2 min read

In Django, related_name is an attribute that can be used to specify the name of the reverse relation from the related model back to the model that defines the relation. It is used to specify the name of the attribute that will be used to access the related model from the reverse side of the relation.

This attribute is particularly useful when working with many-to-many and foreign key relationships.

For example, consider the following models.

class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.ForeignKey(Author, on_delete=models.CASCADE)

In this example, the Book model has a foreign key relationship with the Author model. By default, Django will use the lowercased model name of the related model to create the reverse relation. In this case, the reverse relation would be named author_set.

This means that you can access the books related to an author like this:

author = Author.objects.get(name='John Smith')
books = author.author_set.all()
However, sometimes the default reverse relation name may not be clear or may clash with another attribute. To change the name of the reverse relation, you can use the related_name attribute in the ForeignKey field.

Here's an example:

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='books')
With this change, you can access the books related to an author using the books attribute:

author = Author.objects.get(name='John Smith')
books = author.books.all()

Related Name with Many To Many Relationships

You can also use the related_name attribute when working with many-to-many relationships.

For example, let's say you have the following models:

class Person(models.Model):
    name = models.CharField(max_length=100)

class Group(models.Model):
    name = models.CharField(max_length=100)
    members = models.ManyToManyField(Person)
In this example, the Group model has a many-to-many relationship with the Person model. By default, the reverse relation would be named person_set. However, you can change the name of the reverse relation by using the related_name attribute.

For example:

class Group(models.Model):
    name = models.CharField(max_length=100)
    members = models.ManyToManyField(Person, related_name='member_of')

With this change, you can access the groups related to a person using the member_of attribute:

person = Person.objects.get(name='John Smith')
groups = person.member_of.all()
In summary, related_name is an attribute that can be used to specify the name of the reverse relation in Django models and it's an useful utility to write cleaner code. 


DJANGO

Latest Articles

Latest from djangocentral

Capturing Query Parameters of request.get in Django

In Django, the request object contains a variety of information about the current HTTP request, including the query parameters. Query parameters are a way to pass additional information in the URL and are used to filter or sort data. The request object p…
Read more →

2 min read

Understanding related_name in Django Models

In Django, related_name is an attribute that can be used to specify the name of the reverse relation from the related model back to the model that defines the relation. It is used to specify the name of the attribute that will be used to access the relat…
Read more →

2 min read