[Solved]-Django update without required fields

24👍

UPDATE is possible via 2 requests: PUT and PATCH

PUT updates all the fields of the object on which the operation is to be performed. It basically iterates over all the fields and updates them one by one. Thus, if a required field is not found in the supplied data, it will raise an error.

PATCH is what we call a Partial Update. You can update only the fields that are needed to be changed. Thus, in your case change your request method to PATCH and your work will be done.

3👍

I was facing the same error after many experiments found something, so added all fields in serializer.py in class meta, as shown below –

class Emp_UniSerializer( serializers.ModelSerializer ):
    class Meta:
        model = table
        fields = '__all__'  # To fetch For All Fields
        extra_kwargs = {'std_code': {'required': False},'uni_code': {'required': False},'last_name': {'required': False},'first_name': {'required': False}}

Here, we can update any field, it won’t show error ["This field is required."]

👤Omkar

3👍

Add partial = True argument in serializer as such:

serializer = BookSerializer(book, data=request.data, partial=True)
👤Hatim

2👍

I’m not sure why it is sometimes that you look for hours for the answer, then ask, then immediately find the answer. I simply changed my request to be a ‘PATCH’ instead of a ‘PUT’.

👤Diesel

Leave a comment