如何使用函数基础视图更新对象?
在这里,我使用 PUT 方法更新我的对象。当我获取 api 时。它仅更新那些空白字段。那些已经有一些文本或数据的字段不会更新。
这是我输入的数据:
{
"email": "[email protected]",
"first_name": "yes same", //this field has already value and this is not updating
"middle_name": null,
"last_name": "in",
"age": null,
"gender": null,
"bio": null,
"city": null,
"street_address": null,
"state": "punjab", // this field was null and i gave it some value and it updated
"country": "pakistan" // this field was null and i gave it some value and it updated
}
响应对象:
"data": {
"id": 1,
"email": "[email protected]",
"first_name": "ma",
"middle_name": null,
"last_name": "in",
"age": null,
"gender": null,
"bio": null,
"city": null,
"street_address": null,
"state": "punjab",
"country": "pakistan"
}
我的代码:
@api_view(['GET','PUT'])
@permission_classes([IsAuthenticated])
@csrf_exempt
def update_profile(request,id):
profile = UserProfile.objects.if_obj_exists(id)
print(profile)
if request.method=="GET":
serializer = UserProfileSerializer(profile, many=False)
return Response({"status":"success","message": "Ok", "data": serializer.data},status=status.HTTP_200_OK)
elif request.method=="PUT":
serializer = UserProfileSerializer(instance=profile,data=request.data)
print(serializer.is_valid())
if serializer.is_valid():
serializer.save()
return Response({"data": serializer.data},status=200)
return Response({"status":"fail","message": "something went wrong"},status=400)
Here i'm updating my object with PUT method. When I git the api. It only update those fields which are blank. Those fields which has some text or data already are not updating..
this is data i put :
{
"email": "[email protected]",
"first_name": "yes same", //this field has already value and this is not updating
"middle_name": null,
"last_name": "in",
"age": null,
"gender": null,
"bio": null,
"city": null,
"street_address": null,
"state": "punjab", // this field was null and i gave it some value and it updated
"country": "pakistan" // this field was null and i gave it some value and it updated
}
Object in response :
"data": {
"id": 1,
"email": "[email protected]",
"first_name": "ma",
"middle_name": null,
"last_name": "in",
"age": null,
"gender": null,
"bio": null,
"city": null,
"street_address": null,
"state": "punjab",
"country": "pakistan"
}
My code :
@api_view(['GET','PUT'])
@permission_classes([IsAuthenticated])
@csrf_exempt
def update_profile(request,id):
profile = UserProfile.objects.if_obj_exists(id)
print(profile)
if request.method=="GET":
serializer = UserProfileSerializer(profile, many=False)
return Response({"status":"success","message": "Ok", "data": serializer.data},status=status.HTTP_200_OK)
elif request.method=="PUT":
serializer = UserProfileSerializer(instance=profile,data=request.data)
print(serializer.is_valid())
if serializer.is_valid():
serializer.save()
return Response({"data": serializer.data},status=200)
return Response({"status":"fail","message": "something went wrong"},status=400)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 POST 方法:
Try with POST Method: