2π
Do you definitely need it to be a queryset? If you only need it to be an iterable, a simple expression for your purposes is:
profiles = [r.profile for r in thingie.relation_set.all()]
Iβm not sure if a list comprehension counts as irritating iterating, but to me this is a perfectly intuitive, pythonic approach. Of course if you need it to be a queryset, youβre going to do something messier with two queries, eg:
relation_values = thingie.relation_set.all().values_list('pk', flat=True)
profiles = Profile.objects.filter(relation__in=relation_values)
See the βinβ documentation for more. I prefer the first approach, if you donβt need a queryset. Oh and if you only want distinct profiles you can just take set(profiles)
in the first approach or use the distinct()
queryset method in the second approach.
0π
Did you read the Django doc on Making queries? It has a simple example of achieving what you want, more specifically Lookups that span relationships. Make sure you refer the code snippets in the latter link to the model code at the beginning of the page.
- [Answered ]-Model Form wont validate with OneToOneField(User)
- [Answered ]-How do I send a JavaScript confirm response over Django to a script to perform operations based on the response?
- [Answered ]-Google app engine and django templates: passing entity ids to the template engine
- [Answered ]-Django Model Field Uniqueness Validation
- [Answered ]-Django is unable to load angular chunks
0π
Without defining a direct relationship between Profile and Thingie, you canβt.
The best way would be to add a ManyToMany to Thingie that points to Profile and use the through
argument (django docs) on the ManyToMany to specify your Relation model/table.
That way you can do a direct filter operation on Thingie to get your profiles and still store your intermediate Relation data.
- [Answered ]-Django inheritance, relationship between classes who depends directly/indirectly of same class
- [Answered ]-Can't delete user after removing app
- [Answered ]-NoReverseMatch at /rango/ newbie got stuck in tango w django tutorial