假设有一个列表
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])
关于python - Django过滤器: can filter with tuple?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13441539/