[Answered ]-Creating dynamic objects with | separator python

2👍

import operator
names = [...]
query = reduce(operator.or_, [Q(name__icontains=name) for name in names])
results = queryset.complex_filter(query)

0👍

I do not know what Q is in this case, but maybe

import operator
qq = [Q(name__contains=i) for i in name)]
args = reduce(operator.or_, qq)

might help. But as this is the same as Timmy wrote, don’t upvote me, but him.

If not, see at this question here.

👤glglgl

0👍

Here is a pretty satisfying solution. If it is ever of any help to anyone

http://bradmontgomery.blogspot.com/2009/06/adding-q-objects-in-django.html

q = Q(content__icontains=term_list[0]) | Q(title__icontains=term_list[0])
for term in term_list[1:]:
    q.add((Q(content__icontains=term) | Q(title__icontains=term)), q.connector)

stories = stories.filter(q)

Leave a comment