I have a model field with choices charfield
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 charfield for status but with source
argument 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 through patch request with data {'status': "U"}
, there is no update performed.
However the update occurs when I remove the source
from serializer status field.
Providing source is necessary to display proper value in web view.
I know the option of changing the name of status in serializer to something other and using that in the template. Also there is the option to override update method in serializer, however my question is what is source doing to prevent the update?
I think you need to add status to the fields list in meta.
class VehicleSerializer(ModelSerializer):
status = serializers.CharField(source='get_status_display')
class Meta:
model = Vehicle
fields = ('status',)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With