你不能在访问`serializer.data'后调用`.save()'。

3 人关注

从API发布数据时显示错误

You cannot call `.save()` after accessing `serializer.data`.If you need to access data before committing to the database then inspect 'serializer.validated_data' instead. 

My written Code is:

serializerdata = serializers.CreateSerializer(data=request.data) if serializerdata.is_valid(): user_id = serializerdata.data.get('user_id') if user_id==2: serializerdata.save(i_created_by=request.user) return JsonResponse({"message": "success"}) else: return JsonResponse({"message": "user invalid"}) else: return JsonResponse({"message": "error"})
1 个评论
How does the request.data looks like?
python
django
python-3.x
django-rest-framework
Chandan Sharma
Chandan Sharma
发布于 2019-10-24
2 个回答
Nico Griffioen
Nico Griffioen
发布于 2022-09-21
已采纳
0 人赞同

正如错误所说,你不能访问 serializer.data ,然后再调用 save()

你应该改用 validated_data