Django What is reverse relationship?
NickName:name Ask DateTime:2013-06-27T03:34:44

Django What is reverse relationship?

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 ?

Copyright Notice:Content Author:「name」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/17328910/django-what-is-reverse-relationship

Answers
karthikr 2013-06-26T19:43:15

Here is the documentation on related_name\n\nLets say you have 2 models\n\nclass Group(models.Model):\n #some attributes\n\nclass Profile(models.Model):\n group = models.ForeignKey(Group)\n #more attributes\n\n\nNow, 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. \n\nDjango, 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. \n\nHowever, you can override it by specifying a related_name in the ForeignKey field.\n\nclass Profile(models.Model):\n group = models.ForeignKey(Group, related_name='profiles')\n #more attributes\n\n\nNow, you can access the foreign key as follows:\n\ngroup.profiles.all()\n",


Nishant Yadav 2021-02-08T20:32:45

For a clearer picture you can assume that when we use reverse relationship, it adds an extra field in the referenced model:\nFor example:\nclass Employee(models.Model):\n name = models.CharField()\n email = models.EmailField()\nclass Salary(models.Model):\n amount = models.IntegerField()\n employee = models.ForeignKey(Employee, on_delete=models.CASCADE, related_name='salary')\n\nAfter using related_name in Salary model, now you can assume the Employee model will have one more field: salary.\nFor example, the available fields would now be:\nname, email, and salary\nTo find an employee, we can simply query in this way:\ne = Employee.objects.filter(some filter).first()\nTo check their salary, we can check it by writing\ne.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.\nIn case of many to many relationship we can use .all() and then iterate over that.",


More about “Django What is reverse relationship?” related questions

Django What is reverse relationship?

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? ...

Show Detail

Django comments reverse relationship

When using django.contrib.comments is there anyway to add the reverse relationship to a model that has comments? For example: post = Post.objects.all()[0] comments = post.comments.all()

Show Detail

Django ManyToOne reverse relationship allows null values

I know that a reverse relationship can have a null value, now I am asking if I can force the reverse relationship to be not null. Very simple example: class Subject(models.Model): pass class

Show Detail

Django Admin, accessing reverse many to many

I would like to have access to reverse many to many relationship on Django Admin (django.contrib.admin). class Company(models.Model): name = models.CharField(max_length=100) status = models.

Show Detail

How to create a record from one to one relationship reverse direction in Django?

Django offers Related objects for oneToMany or manyToMany relationship. Using this object, It can create a record from the reverse direction. (for example with XXXX_set.create(.....) or XXXX_set.

Show Detail

Annotate a django query via a reverse relationship

I have two models Property and Image, related by foreign key such that each Property has multiple instances of Image. I'm trying to retrieve a queryset of all the properties - listing all fields - ...

Show Detail

Django query reverse relationship without foreign key

I'm currently working on a Django library to manage images of multiple resolutions (django-multires) where I am stuck optimizing a reserve relationship query. Before I explain the issue, let me try...

Show Detail

Django Model reverse relationship having a string with the name of it

I want to implement a Django Rest Framework view that returns the dependencies tree of a model instance object. This is the code for such view: class RoomTypeDependencies(viewsets.ViewSet): de...

Show Detail

Django tastypie save reverse GenericForeignKeyField

Django Tastypie can save related objects even with reverse relationship. But is it able for Django Tastypie to save reverse relationship of GenericForeignKeyField? My resources (not full, but the

Show Detail

Performance impact of reverse relationships in Django

I'm setting up my Models and I'm trying to avoid using ManyToMany Relationships. I have this setup: Model: Human Some Humans (a small percentage) need to have M2M relationships with other Humans. ...

Show Detail