Skip to content
Advertisement

How do I write a Django ORM query for the reverse relationship in a one-to-many relationship?

I’m using Django and Python 3.7. I have the following two models in my models.py file …

I would like to write a Django ORM query where I select articles have a stat that’s at least 5 minutes (300 seconds) old. However, I don’t know how to reference the ArticleStat object from the Article object. Unsurprisingly, this

produces a

error.

Edit: Per the answer, I changed my ArticleStat model to

and then I ran the below query getting the error displayed

Thought maybe there was an instance with plurality, so I tried an “s”, but got an error ..

Advertisement

Answer

The core issue here is the NameError for articlestat so I will address that first.

As explained in the django documentation your backward relation name by default is defined as FOO_set which in your case means articlestat_set.

If a model has a ForeignKey, instances of the foreign-key model will have access to a Manager that returns all instances of the first model. By default, this Manager is named FOO_set, where FOO is the source model name, lowercased.

If you prefer a different name you can do so by specifying a related_name in your ForeignKey definition e.g.


The second issue is how to properly follow relations which is explained quite extensively here which is why I will not go into detail about it in this answer. The gist is that instead of the . operator you want to use __ (double underscore). The same goes for field lookups which you need for comparison in this query.

With both these issues fixed your query should look like this:

or with a custom related_name e.g. related_name='articlestats':

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement