5👍
✅
A simple rule with Django queries is to always query on the thing you want to get back. So if you want a Class3
, start your query with that.
Class3.objects.filter(class2__class1__search_field=search_criterion).latest()
4👍
Rewrite your code to:
q = Class3.objects.filter(class2__class1__search_field=search_criteria)
I.e. directly ask for the objects you really want, and use the reverse relationships from Class3 back to Class2 and Class1 as part of your filter criteria.
It may help to set related_name
or related_query_name
on the ForeignKey
s that you are using – if set, these are the names to use in the filter() arguments. If not set, they default to the class name.
- [Django]-TypeError: add() argument after * must be a sequence, not Subscribers
- [Django]-Django inline – allow adding disable editing
Source:stackexchange.com