gpt4 book ai didi

python - 访问 `.save()`后不能调用 `serializer.data`

转载 作者:行者123 更新时间:2023-12-05 02:58:53 25 4
gpt4 key购买 nike

从 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. 

我写的代码是:


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"})

最佳答案

如错误所述,您无法访问 serializer.data 然后调用 save()

您应该改为用户 validated_data:

user_id = serializerdata.validated_data.get('user_id')

关于python - 访问 `.save()`后不能调用 `serializer.data`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58538306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com