0

I want Update the data to use put request update the data by using Employee Id Value first retrieve the data which we pass id number and update that particular Id data

        def put(self, request, pk):
        employeeid = self.get_object(pk)
        serializer = employeeSerializer(employeeid, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

I have this sample code but I didn't get update at particular id number. can anyone help for that ques.

path('employees/<int:id>/',views.employeeList.as_view())

The above one is url

4
  • is there id field in your serializer? And do you send id inside your request body? Commented Feb 18, 2020 at 8:22
  • Where's the id / pk value in input function ? Or is it part of input json ? Share more code for your model, serializer and input url u r trying. Or you can follow this basic tutorial : coding-sessions.com/series/drf (check the views part 6). Commented Feb 18, 2020 at 9:08
  • yes the Id filed in serializer. I didn't give in request body Commented Feb 18, 2020 at 9:43
  • not enough code to help. Are you calling the correct class in url route ? Are you sending input json as request data and http method as put ? You can put Print statements in your function for debug. Commented Feb 18, 2020 at 12:43

1 Answer 1

1

You can write in following way to update the values. refer the below link https://www.django-rest-framework.org/api-guide/serializers/#dynamically-modifying-fields

    def update(self, instance, validated_data):
        '''
        employee updation
        '''
        super(self.__class__, self).update(instance, validated_data)
        return instance

        class Meta:

            model = Employee 
            fields = ('__all__')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.