Skip to content

Instantly share code, notes, and snippets.

@nara-l
Created October 25, 2018 21:31
Show Gist options
  • Save nara-l/b1b336386a6fc7209d54467a4eefa2de to your computer and use it in GitHub Desktop.
Save nara-l/b1b336386a6fc7209d54467a4eefa2de to your computer and use it in GitHub Desktop.
Django What is reverse relationship?
`class Blog(models.Model):
#some attributes
class Entry(models.Model):
blog = models.ForeignKey(Group, related_name='entries') . # this is a ForeignKey relationship, not a OneToOneField
#more attributes
`
# Access entries related to blog
blog.entres.all()
# access 1 entry related to blog
blog.entries.all()[0]
# access entries related to blog when related_name is not set
blog.entries_set.all() # not tested
# access 1 entry related to blog
blog.entries_set.all()[0] # not tested
https://docs.djangoproject.com/en/1.11/topics/db/queries/#following-relationships-backward
https://stackoverflow.com/questions/17328910/django-what-is-reverse-relationship
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment