django过滤器:可以用元组过滤吗?

时间:2012-11-18 15:30:34

标签: python django filter

假设有一个列表

strings = ['a','b','c']

并且有两个模型

class theModel:
   theString = models.charField()

Class superModel:
   hasClass = models.ForeignKey(theModel)

有没有办法用'theString'用列表过滤superModel?

例如,这可以是一种方式(但有没有更好的方法?没有for循环)

tuple = []
for string in strings
   tuple.append ( theModel.objects.filter(theString = string) )

result = []
for theModel in tuple 
   result.append ( superModel.objects.filter(hasClass = theModel ) )

return result

1 个答案:

答案 0 :(得分:3)

你可以这样做:

theModel.objects.filter(theString__in=[1,4,7])