3👍
✅
I don’t believe it is possible to do what you are asking.
The way you do ORs in django is like this:
Model.objects.filter(Q(question__startswith='Who') | Q(question__startswith='What'))
so if you actually wanted to do this:
Model.objects.filter(Q(language='de') | Q(language='en'))
you would need to put them both in the same filter() call so you wouldn’t be able to add the other or clause in a later filter() call.
I think the reason you may be trying to do this would be that you are concerned about hitting the database again but the only way to get accurate results would be to hit the database again.
If you are simply concerned about producing clean, DRY code, you can put all the filters that are common to both queries at the top and then “fork” that query set later, like this:
shared_qs = Model.objects.filter(active=True)
german_entries = shared_qs.filter(language='de')
german_and_english = shared_qs.filter(Q(language='de') | Q(language='en'))
Source:stackexchange.com