2012-11-18 142 views
1

假设,有一个列表django过滤器:可以过滤元组?

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

有两种模式

class theModel: 
    theString = models.charField() 

Class superModel: 
    hasClass = models.ForeignKey(theModel) 

有什么办法来过滤用清单“theString”超级名模?

例如这可能是一个办法(但有没有更好的办法,而不用于循环?)

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 

回答

3

你可以这样做:

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