I have a model with a status field that looks something like this:
PENDING = 'pending'
DONE = 'done'
CANCELED = 'canceled'
class Event:
EVENT_STATUSES = [(1, PENDING), (2, DONE), (3, CANCELED)]
status = models.CharField(max_length=20, choices=EVENT_STATUSES, default=PENDING)
I have a serializer:
class EventUpdateSerializer(serializers.ModelSerializer):
class Meta:
model = Event
fields = ('status')
And when it is called with the following JSON:
{
"status": "done"
}
I get the response:
{
"status": [
"\"done\" is not a valid choice."
]
}
I think the problem is the escaping of done, but why? And how do I prevent it?
(I have a content-type application/json header).
Here you are making them as 1,2,3 it will store the 1,2,3 in Database. that's why you are getting error.
You can either try with 1,2,3 (that means you will have to send 1,2 or 3 for it to be a valid choice ) or store them as correct values in the DB/Model
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