如何编写搜索嵌套 JSON 数组的 Django 查询?

问题描述 投票:0回答:1

我的 Draft 模型中有以下 JSON 对象(名为“数据”):

DRAFT1 data=
{
    communications = [
                        {name: "john"},
                        {name: "bobby"}
                     ]
}


DRAFT2 data=
{
    communications = [
                        {name: "mark"},
                        {name: "erin"}
                     ]
}

我猜这对于查询来说是模糊正确的,但我无法弄清楚“*”的语法?

Draft.objects.filter(data__communications__*__name__in=['john', 'erin']).count()

如何编写 Django/Postgres 查询以返回 DRAFT1 和 DRAFT2 作为结果?

json django postgresql django-queryset
1个回答
0
投票

您可以使用 contains 作为通信数组:

from django.db.models import Q

Draft.objects.filter(Q(data__communications__name='john') | Q(data__communications__name='erin')).distinct()
© www.soinside.com 2019 - 2024. All rights reserved.