我的 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 作为结果?
您可以使用 contains 作为通信数组:
from django.db.models import Q
Draft.objects.filter(Q(data__communications__name='john') | Q(data__communications__name='erin')).distinct()