user13729875
user13729875

Reputation:

how to update one only one field

I'm working on a small project using Django / Rest Framework. I would like to update my table ( since I have already some rows ) I want to update only one column status but I get an error about other fields are required:

how can I update only one field, exactly status, I want to set the status to 1, without updating the other columns,
(Django keep telling me other fields are required like title, mobile)

this is my code :

obj = Task.objects.filter(id=tasks['id']).first()
       serializer = self.serializer_class(obj, data = {'status':1})
       if serializer.is_valid():
          serializer.save()

Upvotes: 0

Views: 3022

Answers (1)

Sebri Issam
Sebri Issam

Reputation: 46

As mentioned in the official Django documentation, serializer must passed values for all required fields. in order to omit this behavior you need to assign True to the partial attributes.

obj = Task.objects.filter(id=tasks['id']).first()
serializer = self.serializer_class(obj, data = {'status':1}, partial=True)
if serializer.is_valid():
    serializer.save()

Upvotes: 3

Related Questions