更新时间:2023-11-30 23:20:16
您可以使用 JsonResponse 与 值.简单例子:
from django.http import JsonResponsedef some_view(请求):data = list(SomeModel.objects.values()) # 包裹在 list() 中,因为 QuerySet 不是 JSON 可序列化的return JsonResponse(data, safe=False) # 或 JsonResponse({'data': data})
或者使用 Django 的内置序列化程序的另一种方法:
from django.core 导入序列化程序从 django.http 导入 HttpResponsedef some_view(请求):qs = SomeModel.objects.all()qs_json = serializers.serialize('json', qs)返回 HttpResponse(qs_json, content_type='application/json')
在这种情况下,结果略有不同(默认不缩进):
[{"model": "some_app.some_model",PK":1,领域":{"name": "埃隆",年龄":48,...}},...]我不得不说,使用 marshmallow 之类的东西来序列化查询集是一种很好的做法.
...以及一些提高性能的注意事项:
objects.values()
指定必填字段列表以避免序列化和发送到客户端不必要的模型字段(您也可以将 fields
传递给 serializers.序列化
);I want to serialize my queryset, and I want it in a format as this view outputs:
class JSONListView(ListView):
queryset = Users.objects.all()
def get(self, request, *args, **kwargs):
return HttpResponse(json.dumps({'data': [['bar','foo','bar','foo'],['foo','bar','foo','bar']]}, indent=4), content_type='application/json')
I simply don't know how to output the queryset instead of the manual data in the example.
I've tried
json.dumps({"data": self.get_queryset()})
and
serializers.serialize("json", {'data': self.get_queryset()})
but it wont work. What am I doing wrong? Do I need to make a custom JSON Encoder?
You can use JsonResponse with values. Simple example:
from django.http import JsonResponse
def some_view(request):
data = list(SomeModel.objects.values()) # wrap in list(), because QuerySet is not JSON serializable
return JsonResponse(data, safe=False) # or JsonResponse({'data': data})
Or another approach with Django's built-in serializers:
from django.core import serializers
from django.http import HttpResponse
def some_view(request):
qs = SomeModel.objects.all()
qs_json = serializers.serialize('json', qs)
return HttpResponse(qs_json, content_type='application/json')
In this case result is slightly different (without indent by default):
[
{
"model": "some_app.some_model",
"pk": 1,
"fields": {
"name": "Elon",
"age": 48,
...
}
},
...
]
I have to say, it is good practice to use something like marshmallow to serialize queryset.
...and a few notes for better performance:
objects.values()
to specify list of required fields to avoid serialization and sending to client unnecessary model's fields (you also can pass fields
to serializers.serialize
);