David542
David542

Reputation: 110163

Django query using OR

I have a queryset of providers:

>>> provider_qs = Provider.objects.filter(...)
[<Provider: Gin Investors>, <Provider: IND INVESTORS>]

What would be the query to get all financial statements for those providers? Something like -

>> fs = FinancialStatement.objects.filter(provider__in provider_qs)

?

Upvotes: 0

Views: 77

Answers (2)

seler
seler

Reputation: 9193

You can do this in one line:

fs = FinancialStatement.objects.filter(provider__whatever__more='asd')

or using example from Mitar's answer:

entries = Entry.objects.filter(blog__name__contains='Cheddar')

You should really read more on Django querysets.

Upvotes: 0

Mitar
Mitar

Reputation: 7030

Yes, you can simply do:

fs = FinancialStatement.objects.filter(provider__in=provider_qs)

Django optimizes this into one SQL query. There is an example exactly for this in the Django QuerySet documentation:

inner_qs = Blog.objects.filter(name__contains='Cheddar')
entries = Entry.objects.filter(blog__in=inner_qs)

Upvotes: 2

Related Questions