Django filter()

from functools import reduce

lst = [Q(question_text__contains='you'), Q(question_text__contains='who')]

# OR
>>> Question.objects.filter(reduce(operator.or_, lst))
[<Question: what are you doing>, <Question: what is wrong with you>, <Question: who are you>, <Question: who am i>]

# AND
>>> Question.objects.filter(reduce(operator.and_, lst))
[<Question: who are you>]


>>> from functools import reduce
>>> reduce(lambda x, y: x + y, [1,2,3,4,5,6,7,8])
36
Django filter()Django filter() Xingewud1 发布了38 篇原创文章 · 获赞 2 · 访问量 1051 私信 关注
上一篇:让你的Python健步如飞的小技巧


下一篇:lru_cache总结