Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

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()

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

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access serializer.data, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read