Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django What is reverse relationship?

Tags:

python

django

Can someone tell me what is reverse relationship means? I have started using Django and in lot of places in the documentation I see 'reverse relationship, being mentioned. What is it exactly mean? why is it useful? What does it got to do with related_name in reference to this post ?

like image 557
name Avatar asked Jun 26 '13 19:06

name


People also ask

What is reverse relationship Django?

Thats' where related name or the reverse relationship comes in. Django, by defaults gives you a default related_name which is the ModelName (in lowercase) followed by _set - In this case, It would be profile_set , so group. profile_set . However, you can override it by specifying a related_name in the ForeignKey field.

What is a QuerySet in Django?

A QuerySet is a collection of data from a database. A QuerySet is built up as a list of objects. QuerySets makes it easier to get the data you actually need, by allowing you to filter and order the data.

What does related name mean in Django?

The related_name attribute specifies the name of the reverse relation from the User model back to your model. If you don't specify a related_name, Django automatically creates one using the name of your model with the suffix _set, for instance User.

How does Django store data in database?

Creating objectsTo create an object, instantiate it using keyword arguments to the model class, then call save() to save it to the database. This performs an INSERT SQL statement behind the scenes. Django doesn't hit the database until you explicitly call save() . The save() method has no return value.


2 Answers

Here is the documentation on related_name

Lets say you have 2 models

class Group(models.Model):     #some attributes  class Profile(models.Model):     group = models.ForeignKey(Group)     #more attributes 

Now, from a profile object, you can do profile.group. But if you want the profile objects given the group object, How would you do that? Thats' where related name or the reverse relationship comes in.

Django, by defaults gives you a default related_name which is the ModelName (in lowercase) followed by _set - In this case, It would be profile_set, so group.profile_set.

However, you can override it by specifying a related_name in the ForeignKey field.

class Profile(models.Model):     group = models.ForeignKey(Group, related_name='profiles')     #more attributes 

Now, you can access the foreign key as follows:

group.profiles.all() 
like image 186
karthikr Avatar answered Sep 29 '22 08:09

karthikr


For a clearer picture you can assume that when we use reverse relationship, it adds an extra field in the referenced model:

For example:

class Employee(models.Model):            name = models.CharField()            email = models.EmailField() class Salary(models.Model):            amount = models.IntegerField()            employee = models.ForeignKey(Employee, on_delete=models.CASCADE, related_name='salary') 

After using related_name in Salary model, now you can assume the Employee model will have one more field: salary.

For example, the available fields would now be:

name, email, and salary

To find an employee, we can simply query in this way:

e = Employee.objects.filter(some filter).first()

To check their salary, we can check it by writing e.salary (now we can use salary an attribute or field in employee model). This will give you the salary instance of that employee, and you can find the amount by writing e.salary.amount. This will give you the salary of that employee.

In case of many to many relationship we can use .all() and then iterate over that.

like image 45
Nishant Yadav Avatar answered Sep 29 '22 06:09

Nishant Yadav