从Django JSONField

时间:2018-05-17 13:51:54

标签: django

我试图从我的模型中的JSONField中检索值,但是我收到了错误:

'阿比'对象没有属性& openapi_spec__info__title'

class Api(models.Model):
''' Model to hold infomation on the API '''

# JSONB field to hold the OpenAPI spec
openapi_spec = JSONField()

# Derive product name from the OpenAPI spec, since it's a mandatory field
def _get_product_name(self):
    return self.openapi_spec__info__title
product_name = property(_get_product_name)

根据Django文档' __'是过滤JSON数据的正确方法,但是它可能不是正确的访问方式吗?

{"info": {"title": "Test API", "version": "1.0.0"}}

1 个答案:

答案 0 :(得分:2)

双下划线语法仅在过滤器和排序等方面有效,无论您是处理JSON字段还是传统的跨表连接。

该字段为您提供字典。您可以使用正常的字典语法。

self.openapi_spec['info']['title']