如何使用 Postgres 在 Django 中的 JSONField 中搜索带有空格的键?

del*_*rce 2 python django django-models django-postgresql

假设我在 Django 的 model.py 之一中定义了一个 JSONField,即“数据”。该字段的内容有点类似于以下内容 -

{
    "name": "John",
    "email": "johndoe@foo.bar",
    "last name": "Doe"
}
Run Code Online (Sandbox Code Playgroud)

我需要编写以下形式的查询 -

self.objects.filter(data__name="John")
Run Code Online (Sandbox Code Playgroud)

如何为键“姓氏”编写类似的查询。我无法继续,因为该键有空格。正在考虑使用 python 获取数据并对其进行过滤,但我认为会有更有效的方法来完成它。

我无法真正控制 JSONField 中的数据。所以,我无法真正更改密钥的名称。

del*_*rce 7

我们可以通过以下方式实现它 -

lookup = "data__last name"
self.objects.filter(**{lookup: "Doe"})
Run Code Online (Sandbox Code Playgroud)