我在Postgres db中有一个jsonfield,数据如下:
income_info = [
{
"id": "1",
"name": "A",
"min_income": 22000
},
{
"id": "2",
"name": "B",
"min_income": 40000
},
{
"id": "3",
"name": "C",
"min_income": 22000
}
]
现在想在Django orm查询集上使用gte和lte。已经尝试过
Employee.objects.filter(income_info__min_income__lte = 4000000)
但根本不起作用。
models.py:
class Employee(models.Model):
institute = models.ForeignKey(Institute, on_delete=models.DO_NOTHING)
income_info = JSONField(default=list)
others = models.TextField(null=True)
答案 0 :(得分:1)
在Django的documentation中用于查询JsonFields:
如果键是整数,它将被解释为数组中的索引查找
由于您的json数据是json数据列表,因此您需要这样的查询:
Employee.objects.filter(income_info__0__min_income__lte=4000000)