简体   繁体   中英

How to get value of json field in django views.py

[views.py]

from_date = request.GET.get('from_date')
to_date = request.GET.get('to_date')
histories = History.objects.filter(content__icontains='teacher', date__gte=from_date, date__lte=to_date).order_by('date')
for h in histories:
    histories = History.objects.annotate(teacher=json.loads(h.summary)).values('study_name', 'teacher', 'date')

I am trying to print the 'study_name', 'summary', and 'date' fields of the History model that satisfy the period set in Django views.py. The summary field uses json.loads as the json type, but the following error occurs.

QuerySet.annotate() received non-expression(s): {'field_summary': {'recruiting': 'None -> Yes', 'teacher': 'None -> Halen', 'subject': 'None -> 'science'}, 'file_summary': {}}.

How to get the teacher value of field_summary?

You may try this:

import json
teacher_value = json.load(histories.teacher)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM