In Django, is it possible to make a HttpResponse which is a combination of a queryset and a text string?
I imagine something like this
objs = ModelName.objects.all()
text = "Some text"
allData = ??? #Some kind of operation (json.dumps, serializers, or ...) that combines the two
return HttpResonse(allData,content_type="application/json")
You can wrap both in a dictionary, for example:
from django.http import JsonResponse
from django.core.serializers import serialize
from json import loads as jloads
objs = ModelName.objects.all()
text = 'Some text'
allData = {
'objs': serialize('json', objs),
'text': text
}
return JsonResponse(allData)
The data is thus a JSON object with two keys: objs
that will contain the serialized queryset, and text
that will contain the value in text
.
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.