我正在尝试过滤一个Django JSONfield(MariaDB后端,Django 4.0)
target_360是JSON字段,我尝试的每个查询都返回一个空的queryset,即使调试语句在“父”查询的第一行中清楚地显示了匹配的id
在www.example.com中models.py使用django.models.jSONField创建一个字段
target_360 = models.JSONField(_('360 target'),default=None, null=True)
查询代码
surveys_with_target = Survey_Instance.objects.filter(pulse_id=pulse_id, survey_id=survey_id, target_360__isnull=False)
logger.debug('First row target_360')
logger.debug(surveys_with_target[0].target_360)
logger.debug('target_id in filter')
logger.debug(target_id)
survey_test = surveys_with_target.filter(target_360__contains=target_id)
logger.debug("SURVEY TEST:")
logger.debug(survey_test)
survey_test = surveys_with_target.filter(target_360__id__contains=target_id)
logger.debug("SURVEY TEST 2:")
logger.debug(survey_test)
survey_test = surveys_with_target.filter(target_360__id=target_id)
logger.debug("SURVEY TEST 3:")
logger.debug(survey_test)
编辑-添加了更多测试:
logger.debug("SURVEY TEST 4")
survey_test = surveys_with_target.filter(target_360__contains={'id':target_id})
logger.debug(survey_test)
logger.debug("SURVEY TEST 5")
survey_test = surveys_with_target.filter(target_360__contains={"id": "189f5422-f522-4860-8794-a3375f84a086"})
logger.debug(survey_test)
logger.debug("SURVEY TEST 6")
survey_test = surveys_with_target.filter(target_360__contains={"id": target_id})
logger.debug(survey_test)
logger.debug("SURVEY TEST 7")
survey_test = surveys_with_target.filter(target_360__contains="189f5422-f522-4860-8794-a3375f84a086")
logger.debug(survey_test)
logger.debug("SURVEY TEST 8")
survey_test = surveys_with_target.filter(target_360__has_key="id")
logger.debug(survey_test)
调试输出:
First row target_360
{"id": "189f5422-f522-4860-8794-a3375f84a086", "target_type": "Individual"}
target_id in filter
189f5422-f522-4860-8794-a3375f84a086
SURVEY TEST:
<QuerySet []>
SURVEY TEST 2:
<QuerySet []>
SURVEY TEST 3:
<QuerySet []>
SURVEY TEST 4
<QuerySet []>
SURVEY TEST 5
<QuerySet []>
SURVEY TEST 6
<QuerySet []>
SURVEY TEST 7
<QuerySet []>
SURVEY TEST 8
<QuerySet []>
很可能很简单,我做错了什么?
1条答案
按热度按时间6ie5vjzr1#
你就快找到答案了。试试这个吧
关于如何在Django中查询
JSONField()
的更多细节,这里是docs链接。