在django JSONField中查询嵌套对象列表中的嵌套值

时间:2018-07-06 09:18:41

标签: django postgresql django-jsonfield

我有一个句子模型,其元数据为JSONField
一行示例是

Sentence.objects.filter(id=6753315).values('id', 'metadata')[0]

{'id': 6753315,
 'metadata': [{'filters': [{'id': None, 'level_name': 'Brand Hierarchy'},
                           {'id': None, 'level_name': 'Category Hierarchy'}],
               'product': None,
               'themes': [{'id': 35299, 'sentiment': 'Positive'},
                          {'id': 35301, 'sentiment': 'Positive'}]}]}

元数据是对象列表,并且该对象具有嵌套的对象列表。
我想根据主题ID查询此类行。在此示例中,元数据列表的行长度为1,主题列表的长度为1,因此像

一样易于查询
In [30]: Sentence.objects.filter(id=6753315, metadata__0__themes__0__contains={"id": 35299})
Out[30]: <QuerySet [<Sentence: Sentence object>]>

但是元数据和主题的长度可以是任何长度,因此我如何查询此类行以检查该行中是否存在任何长度的元数据和主题的主题ID。

我只想找出所有具有特定主题ID的行

Sentence.objects.filter(metadata__*__themes__*__contains={"id": 35299})  

我不知道索引是这样写的*来使我的问题易于理解。

0 个答案:

没有答案
相关问题