django QuerySet对象转json

查询多个数据时
categories = WpTerms.objects.all().values()
data = list(categories)
# data = tuple(categories)
# 因为data不是dict,所以需要设置safe=False,
# 不然会报错:In order to allow non-dict objects to be serialized set the safe parameter to False.
return JsonResponse(data, safe=False)

查询单个数据时
data = model_to_dict(WpTerms.objects.get(term_id=id))
return JsonResponse(data, safe=False)

Leave Comment

电子邮件地址不会被公开。 必填项已用*标注