【发布时间】:2025-12-07 18:10:01
【问题描述】:
我有一个包含 2 个字段的简单模型:
class Simple(Model)
class Meta:
index_together = True
a = IntField()
b = IntField()
我想为 a,b 的值元组生成 SQL 查询。
例如
select *
from SimpleModel
where (a,b) in ((1,1), (4,8), ...)
我知道如何创建类似的东西:
select *
from SimpleModel
where ((a = 1 and b = 1) or (a = 4 and b = 8))
这在逻辑上是相同的,但我认为我的数据库在可能值的数量非常大时会出现问题(我使用的是 Postgresql),而且查询本身要长得多,因此它在网络上更重,而且可能更难以便它能够正确分析和读取它(即在这种情况下使用复合索引)。
那么,问题是,我可以让 Django 以第一种形式创建查询吗?
谢谢!
【问题讨论】:
标签: sql django-models