Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escpaing issue with 'Not a valid choice' in Django Rest Framework

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).

like image 346
Uri Shalit Avatar asked Nov 09 '22 02:11

Uri Shalit


1 Answers

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

like image 94
Mayank Pratap Singh Avatar answered Nov 24 '22 21:11

Mayank Pratap Singh