任何人都知道为什么会这样 query_set
不为我返回任何值?单独使用过滤器,效果很好,所以看起来.filter().filter()
一起是过滤“非此即彼”的错误方法。
ticket_query = request.event.tickets.filter(status='on-sale').filter(status='paused').prefetch_related('ticket_tax')
最佳答案
带有多个参数的 filter() 用 AND 语句连接它们:
https://docs.djangoproject.com/en/2.0/ref/models/querysets/#filter
要在 Django 中执行 OR 查询,您可以使用 Q 对象:
from django.db.models import Q
ticket_query = request.event.tickets.filter(Q(status='on-sale') | Q(status='paused')).prefetch_related('ticket_tax')
更多细节在这里:
https://docs.djangoproject.com/en/2.0/topics/db/queries/#complex-lookups-with-q
关于python - Django过滤多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51121661/