What is “related_name” used for in Django

django related_name

related_name is an attribute you use while defining relations like many-to-one, many-to-many, one-to-one etc.

The value of this attribute is used as the name of the reverse relation between models.


Here we define a many-to-one relation using a Foreign Key. A Site can have many Pages. The relation from a Site to its pages can be expressed as such:

A reverse relation can be defined from the Site to Pages as you see above. It is important to understand the importance of related_name attribute. related_name can save you from many headaches by preventing clashes from ever happening in your applications. Different models having a Foreign Key relation to the same “X” model could have clashes for using the same default related_name values.

Explicitly specifying related_name values for your relations will save you a lot of troubles later.

Hope this helps.
Good Luck,