'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?
Solution 1:[1]
You may try this:
import json
teacher_value = json.load(histories.teacher)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Iliano 920 |
