Ad

Django Filter, Many Queries In Array

- 1 answer

need an array where Q() object are added filter with many value

Anime.objects.filter(Q(question__startswith='Who') & Q(question__startswith='What'))

but need something like this

val_arr = []
val_arr.append(Q(question__startswith='Who'))
val_arr.append(Q(question__startswith='What'))
Anime.objects.filter(val_arr)
Ad

Answer

You can do it with dictionary like this:

filters = Q()

val_arr = dict()
val_arr.update({"question__startswith": 'Who'))
val_arr.update({"question__startswith": 'What'))

for item in val_arr:
   filters |= Q(**{item:val_arr[item]})

Anime.objects.filter(filters)
Ad
source: stackoverflow.com
Ad