Object of type 'QuerySet' is not JSON serializable

在Django框架中,我们不能直接将QuerySet对象通过 HttpResponse(json.dumps(QeurySet))返回给前端Ajax....

否则会报错:Object of type 'QuerySet' is not JSON serializable

 

因此需要序列号后才能返回给前端Ajax....

from django.core import serializers

def ajax_value(request):
    ajax_testvalue = serializers.serialize("json", models.MonitorData02DB.objects.all().order_by("-id")[:1])
    return HttpResponse(ajax_testvalue)

 

posted @ 2018-03-22 20:45  Vincen_shen  阅读(5387)  评论(0)    收藏  举报