FAQ: CRUD Functionality - Reverse Relationships

This community-built FAQ covers the “Reverse Relationships” exercise from the lesson “CRUD Functionality”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Build Python Web Apps with Django

FAQs on the exercise Reverse Relationships

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

If I understand it right we have two options to query all instances on the many-side corresponding to a specific instance on the one-side when it comes to many-to-one relationship (i.e. finding all Patient instances relating to the same Owner instance in the exercise):

  1. Through the _set-property querying on the Owner model
  2. Through the filter()-method querying on the Patient model

The query result is the same but is there any scenario when I want to use one option over the other? Is there any best practice recommendation?

I am trying to understand how to do a reverse relationship search in Django.

Link to Material:
(https://www.codecademy.com/paths/build-python-web-apps-with-django/tracks/data-in-django/modules/django-models-and-databases/lessons/django-crud-functionality/exercises/reverse-relationships)

the hint says :

_set property should be preceded by the lowercase name of the model.

The .all() query method should be called at the end to get all instances.

var_name.modelname_set.all()

In [4]: first_owner.Patient_set.all()
-------------------------------------------------------
AttributeError        Traceback (most recent call last)
<ipython-input-4-bd2c78b3f9b7> in <module>
----> 1 first_owner.Patient_set.all()

AttributeError: 'Owner' object has no attribute 'Patient_set'

In [5]: first_owner.Owner_set.all()
-------------------------------------------------------
AttributeError        Traceback (most recent call last)
<ipython-input-5-2068fa21d846> in <module>
----> 1 first_owner.Owner_set.all()

AttributeError: 'Owner' object has no attribute 'Owner_set'

I have attached a screenshot to show what it looks like below. What am I doing wrong?

1 Like