Django'objects.filter()'与列表? [英] Django 'objects.filter()' with list?
本文介绍了Django'objects.filter()'与列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以以这种方式限制QuerySet:
It is possible to limiting QuerySet in this kind of way:
creators_list = ['jane', 'tarzan', 'chita']
my_model.objects.filter(creator=creators_list)
推荐答案
你的意思是这样吗?
my_model.objects.filter(creator__in=creator_list)
文档: http://docs.djangoproject.com/en/dev/ref/models/querysets/#在
编辑
现在有点过时了。如果您遇到原始代码问题,请尝试以下方式:
This is now a bit outdated. If you run into problems with the original code, try this:
from django.db.models import Q
my_filter_qs = Q()
for creator in creator_list:
my_filter_qs = my_filter_qs | Q(creator=creator)
my_model.objects.filter(my_filter_qs)
更好的方式来做,但我目前无法测试。
There's probably a better way to do it but I'm not able to test it at the moment.
这篇关于Django'objects.filter()'与列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文