python - Django过滤器: can filter with tuple?

标签 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

最佳答案

你可以这样做:

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

关于python - Django过滤器: can filter with tuple?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13441539/

相关文章:

python - 在 Seaborn 中创建深色反转调色板

jquery - jqgrid 过滤器在加载时删除一行一次

Matlab,如何按条件过滤数字向量?

python - 在管理员中从 TabularInline 中删除标题

javascript - 检查对象是否有key和value

python - 非标准化负载中心性可以是非整数吗?

python - 气泡图,气泡大小等于Python中的组大小

python - Django obj_create 在我的表单验证之前运行

python - 如何重新创建 Django 项目的数据库?

django - django 模板中的两个外键和一个值