Ryan
Ryan

Reputation: 2660

Django filter that spans relationships but programmatically?

I understand that to span a relationship in a Django filter you use a double underscore like this:

Blog.objects.filter(entry__headline__contains='Lennon')

However, I have a bunch of dynamic filters that will be determined at runtime. Can I achieve the same as the above but programmatically?

Upvotes: 3

Views: 191

Answers (1)

alecxe
alecxe

Reputation: 473863

You can make a dictionary and unpack it into the keyword arguments:

filters = {'entry__headline__contains': 'Lennon'}
Blog.objects.filter(**filters)  

where entry__headline__contains can be dynamically evaluated.

Upvotes: 5

Related Questions