【发布时间】:2020-10-07 17:17:54
【问题描述】:
我希望能够将此过滤器用于“特定”或“空”或“任何值”场景
# 3 scenarios for the third field : Col3
# Query for a specific value
sample = table.objects.filter(Col1=343, Col2=545, Col3=656)
# Query for NULL value
sample = table.objects.filter(Col1=343, Col2=545, Col3=None)
# Query for any value
sample = table.objects.filter(Col1=343, Col2=545, Col3=???)
特定和 NULL 场景由 Python 中的 656 和“无”等精确值处理。 我们如何处理来自上面的任何价值场景?
想象一下我们可以查询数据库的 3 个下拉框。
我的问题与这个类似,但特定于 Django
原始 sql 特定问题在这里:How do you query an int column for any value?,在我的情况下,coloumn 可以是任何类型
生成的查询可能如下所示:
// Query for a specific value
select Col1,Col2,Col3 from table where Col1=343, Col2=545, Col3=656
// Query for NULL value
select Col1,Col2,Col3 from table where Col1=343, Col2=545, Col3 IS NULL
// Query for any value
select Col1,Col2,Col3 from table where Col1=343, Col2=545, Col3=*
【问题讨论】:
-
你试过
Col3__in=[656, None]吗? -
我理解错了吗?所以您想动态过滤 Col3=656、None 或 All?
-
是的,第二条评论是我正在寻找的,它可以是任何值,而不仅仅是 656...
标签: python django django-models