I’m constructing some Django filter queries dynamically, using this example:
kwargs = { 'deleted_datetime__isnull': True }
args = ( Q( title__icontains = 'Foo' ) | Q( title__icontains = 'Bar' ) )
entries = Entry.objects.filter( *args, **kwargs )
I’m just not sure how to construct the entry for args. Say I have this array:
strings = ['Foo', 'Bar']
How do I get from there to:
args = ( Q( title__icontains = 'Foo' ) | Q( title__icontains = 'Bar' )
The closest I can get is:
for s in strings:
q_construct = Q( title__icontains = %s) % s
args.append(s)
But I don’t know how to set up the | condition.
You can iterate it directly using a kwarg format (I don’t know the proper term)
This will look for each string in your
query_stringin each field infieldsand OR the resultI wish I still had my original source for this, but this is adapted from code I use.