Created
October 25, 2018 21:31
-
-
Save nara-l/b1b336386a6fc7209d54467a4eefa2de to your computer and use it in GitHub Desktop.
Django What is reverse relationship?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| `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