Django JSONField过滤Queryset

Irf*_*vic 5 python django postgresql

对于项目,我正在使用Python 3.6.3,Django 2.0和Postgre 9.4。在我的班票中,有一个JSONField乘客

    passenger = JSONField(blank=True)
Run Code Online (Sandbox Code Playgroud)

我的乘客JSON如下所示:

{
    "email": null, 
    "mobile": "21312", 
    "passport": "2141241", 
    "sms_sent": false, 
    "full_name": "something"
},
{
    "email": null, 
    "mobile": null, 
    "passport": "1231231", 
    "sms_sent": false, 
    "full_name": "Irfan"
},
{
    "email": null, 
    "mobile": null, 
    "passport": "1231231", 
    "sms_sent": true, 
    "full_name": "Irfan"
}
Run Code Online (Sandbox Code Playgroud)

现在,我有了django命令,我要在其中过滤具有非null或None且mobile的票证且sms_sent为False的票证。

    tickets = Ticket.objects.filter(
        date=tomorrow, trip__bus_company=bus_company,
        passenger__sms_sent=False
    ).not_cancelled()
Run Code Online (Sandbox Code Playgroud)

现在passenger__sms_sent = False过滤器正在运行,并且正在提供我仅有的sms_sent = False的票证。但是passenger__mobile过滤器不起作用。我尝试了每个人:

    tickets = tickets.exclude(passenger__mobile=None)
    tickets = tickets.exclude(passenger__mobile=None).exclude(passenger__mobile='')
    tickets = tickets.exclude(passenger__mobile__isnull=True)
    tickets = tickets.exclude(passenger__exact={'mobile': None})
    tickets = tickets.exclude(passenger__mobile__isnull=True).exclude(passenger__mobile='')
    tickets = tickets.exclude(passenger__mobile__isnull=False).exclude(passenger__mobile='')
    tickets = tickets.exclude(Q(passenger__mobile__isnull=True) | Q(passenger__mobile=''))
Run Code Online (Sandbox Code Playgroud)

并在第一个过滤器中放置passenger__mobile,但是我无法过滤(排除)passenger__mobile为null的票证,我要么得到所有票证,要么查询集为空。

现在我可以这样做:

for ticket in tickets:
            if ticket.passenger['mobile'] is not None:
                print(ticket.passenger['mobile'])
Run Code Online (Sandbox Code Playgroud)

但这不是我想要的。我想使用过滤器或排除以获取那些票证。我做错了什么?PS not_cancelled()是我的经理,它与乘客领域没有任何关系。

Bob*_*Bob 5

根据此https://code.djangoproject.com/ticket/25718(请参阅最后一个结束语),以下内容应该可以工作 model.objects.filter(field__key=None) (但显然,您应该将Django版本与fix一起使用)。

django文档https://docs.djangoproject.com/zh-CN/2.0/ref/contrib/postgres/fields/#querying-jsonfield

警告

由于任何字符串都可能是JSON对象中的键,因此以下列出以外的任何查找都将被解释为键查找。没有错误。输入错误时要格外小心,并始终按预期检查查询的工作。

这里是https://docs.djangoproject.com/en/2.0/ref/contrib/postgres/fields/#containment-and-key-operations