如何使用函数基础视图更新对象?

发布于 2025-01-09 04:19:04 字数 2050 浏览 0 评论 0原文

在这里,我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩人痒 2025-01-16 04:19:04

尝试使用 POST 方法:

@api_view(['GET','POST'])
@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=="POST":
        serializer = UserProfileSerializer(profile,data=request.data)
       
        if serializer.is_valid():
            serializer.save()
            return Response({"data": serializer.data},status=200)
        return Response({"status":"fail","message": "something went wrong"},status=400)

Try with POST Method:

@api_view(['GET','POST'])
@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=="POST":
        serializer = UserProfileSerializer(profile,data=request.data)
       
        if serializer.is_valid():
            serializer.save()
            return Response({"data": serializer.data},status=200)
        return Response({"status":"fail","message": "something went wrong"},status=400)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文