0

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)

?

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.