代码为:
# 查询所有图书 、 增加图书
def get(self,request):
queryset = BookInfo.objects.all()
book_list = []
for book in queryset:
book_list.append({
'id':book.id,
'bread':book.bread
})
return JsonResponse (book_list,safe=False)
遇到问题:
JsonResponse (book_list, safe=False )
safe=False 这是什么鬼 ?
解决方案:
down 下源码后 :
def __init__(self, data, encoder=DjangoJSONEncoder, safe=True,
json_dumps_params=None, **kwargs):
if safe and not isinstance(data, dict):
raise TypeError(
'In order to allow non-dict objects to be serialized set the '
'safe parameter to False.'
)
if json_dumps_params is None:
json_dumps_params = {}
kwargs.setdefault('content_type', 'application/json')
data = json.dumps(data, cls=encoder, **json_dumps_params)
super(JsonResponse, self).__init__(content=data, **kwargs)
最终答案:
'In order to allow non-dict objects to be serialized set the ' 'safe parameter to False.'

