pythonBug: is not JSON serializable

from django.shortcuts import render,HttpResponse

# Create your views here.
from django.views import View
from .models import Publish
import json


class PublishesView(View):
    def get(self,request):
        publish_list = Publish.objects.all()
        return HttpResponse(json.dumps(publish_list))

    def post(self,request):
        pass

需要修改 publish_list = Publish.objects.all()的数据格式,publish_list数据格式是QuerySet,先强制转换成list而且需要加value,告诉list该如何转
修改后:

publish_list = list(Publish.objects.all().values("name","email"))

数据输出:

[{"email": "123@qq.com", "name": "\u5317\u4eac\u51fa\u7248\u793e"}, {"email": "213@outlook.com", "name": "\u4eba\u6c11\u51fa\u7248\u793e"}]
上一篇:Aspose.Cells for .NET 8.5.0 工具类


下一篇:如何上传自己的npm包和安装上传的npm包并使用