Django框架Object of type 'QuerySet' is not JSON serializable

报错信息如下

Object of type 'QuerySet' is not JSON serializable

报错代码

def queryinfo(request):
    mydata=news.objects.all()
    data = {
        'list': mydata
    }
    return HttpResponse(json.dumps(data), content_type='application/json')

报错原因

queryset没有序列化

解决方案

def queryinfo(request):
    mydata=news.objects.all()
    newsdata= serializers.serialize("json", mydata)
    data = {
        'list': newsdata
    }
    return HttpResponse(json.dumps(data), content_type='application/json')

运行成功效果图 

发布了123 篇原创文章 · 获赞 4 · 访问量 5686

猜你喜欢

转载自blog.csdn.net/tian_jiangnan/article/details/105378929
今日推荐