Django update without required fields
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.
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."]