假设有一个清单

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

最佳答案

你可以这样做:

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

10-04 14:49