【问题标题】:How to query a Django JSONField with a nested dictionary?如何使用嵌套字典查询 Django JSONField?
【发布时间】:2021-05-20 10:33:26
【问题描述】:

我正在使用 Django 3.2.3 / Python 3.7.8 / PostgreSQL 12.3

models.py

class Booking(models.Model):
    reference = models.CharField(max_length=15, unique=True)
    services = models.JSONField()

services 结构是:

{
'1':{
    'id': 3,
    'name': 'Name 1'
    },
'2':{
    'id': 4,
    'name': 'Name 2
    },
'3':{
    'id': 3,
    'name': 'Name 3
    },
 ...
}

如何过滤Bookingservicesid 3

我试过Booking.objects.filter(services__contains=[{'id': 3}]),但打不通。

请帮忙。

【问题讨论】:

    标签: python django jsonfield


    【解决方案1】:

    我认为你应该尝试is_null 查找。

    而不是contains,如下:

    Booking.objects.filter(services__3__isnull=False)
    

    另外我建议您查看 django JSON 查询集文档here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-15
      • 1970-01-01
      • 1970-01-01
      • 2016-06-29
      • 1970-01-01
      • 2019-07-28
      • 2020-07-20
      • 1970-01-01
      相关资源
      最近更新 更多