1

假设有一个列表

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
4

1 回答 1

3

你可以这样做:

theModel.objects.filter(theString__in=[1,4,7])
于 2012-11-18T15:37:03.317 回答