Django Rest Framework charfield serializer not updating when source is specified
I have a model field with a charfield selection
class Vehicle(models.Model):
name = models.CharField(max_length=100)
STATUS_CHOICES = (
("N", "New"),
("U", "Used"),
("P", "Just Purchased")
)
status = models.CharField(max_length=3, choices=STATUS_CHOICES)
The serializer class also has a charfield for status, but with an argument source
to display the readable value
class VehicleSerializer(ModelSerializer):
status = serializers.CharField(source='get_status_display')
class Meta:
model = Vehicle
When I try to update vehicles via a data patch request {'status': "U"}
, the update fails. However, the update happens when I remove source
from the serializer status field. Providing a source is required to display the correct value in the web view.
I know the option to change the status name in the serializer to something else and use that in the template. It is also possible to override the update method in the serializer, however my question is, what is the source doing to prevent the update?
source to share