Как обновить объект с помощью представления базы функций?

Здесь я обновляю свой объект с помощью метода PUT. Когда я использую git api. Он обновляет только те поля, которые пустые. Те поля, в которых уже есть текст или данные, не обновляются.

вот данные, которые я поместил :

{
        "email": "main@gmail.com",
        "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": "main@gmail.com",
        "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)

Попробуйте с методом 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)
Вернуться на верх