4

I have User model with food_prefrence field for which a user has the option to select multiple choices.

In models, I am using MultiSelectField from django-multiselectfield to solve my problem. and in my User serializer, I am using fields.MultipleChoiceField provided by rest-framework.

now my problem is how to get input from the user using form-data and how to process that in my view or serializer, as of now when I am trying to insert choices using postman with form-data selected, this is giving me an error when serializer.is_valid() is called

{
    "food_preference": [
        "\"'Indian', 'Continental'\" is not a valid choice."
    ]
}

below is my code snippet.

#models.py
class User(AbstractUser, BaseClass):
    food_preference = MultiSelectField(_('Food Preference'), choices=CONST_Food, blank=True, null=True)

#serializer.py
class UserSerializer(serializers.HyperlinkedModelSerializer):
    food_preference = fields.MultipleChoiceField(choices=CONST_Food, required=False)

def update(self, instance, validated_data):
    instance.food_preference = validated_data.get('food_preference', instance.food_preference)
    instance.save()
    return instance, "Updated Successfully"

#views.py
def update(self, request, *args, **kwargs):
    instance = self.get_object()
    serializer = self.serializer_class(data=request.data, context={"request": self.request})
    print(serializer.initial_data)
    if serializer.is_valid(raise_exception=True): ##<<<<<Execution stops here
        print("is valid")
        result = serializer.update(instance=instance, validated_data=request.data)
        if result[0] is None:
            return _error_response(400, EPHISH, result[1], {})
        data = self.serializer_class(result[0], context={"request": self.request}).data
        return _response(data, status.HTTP_201_CREATED)
    else:
        return _einval_serializer_response(status.HTTP_400_BAD_REQUEST, self.serializer_class)

also here is the screenshot from my postman enter image description here

Sumeet Kumar
  • 969
  • 1
  • 11
  • 22

2 Answers2

1

I had the same problem. And in DRF i changed the field to:

food_preference = fields.CharField(required=False)

Then i was good to go.

jakobdo
  • 1,282
  • 14
  • 20
  • 1
    this is definitively the work around. i added `fields.MultipleChoiceField(choices=MY_CHOICES)` in my serializer as mentioned in https://pypi.org/project/django-multiselectfield/ and i did not worked. this is weird!!! – Nwawel A Iroume Jun 23 '19 at 14:28
0

If you have something like

CONST_Food= (
        (1, 'One'),
        (2, 'Two'),
        (3, 'Three')
    )

Then you have to send 1,2,3.

<select multiple>
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>

So the idea is to send values.

Update

Oh, finally I got this! You are using django-multiselectfield (please add it to the tags, so if someone will have the same problem he will find the solution faster) I've replicated your configuration and first of all instead of

CONST_Food= (
            (1, 'One'),
            (2, 'Two'),
            (3, 'Three')
        )

your tuple have to look like

CONST_Food= (
            ('1', 'One'),
            ('2', 'Two'),
            ('3', 'Three')
        )

The problem was in the serializer that expects list of strings, but you was sending the list of integers and/or values itself that it can't parse. So your JSON will be

{
...
    "food_preference": ["1", "3"]
...
}
urDMG
  • 428
  • 1
  • 6
  • 14
  • Tried that, giving same error. 1 & 2 is not a valid choice – Sumeet Kumar Mar 02 '18 at 12:25
  • I tried adding django-multiselectfield as tag, but don't have enough reputation to create new tag. You see. – Sumeet Kumar Mar 03 '18 at 05:58
  • @SumeetKumar Please show your CONST_Food and your JSON that you are sending – urDMG Mar 03 '18 at 19:41
  • not sending in JSON format, but in `multipart/form-data`, as my original serializer contain images and also i am using parser for that. – Sumeet Kumar Mar 03 '18 at 21:09
  • @SumeetKumar Please check this, now I'm not sure that this qustion is related to the drf or django but postman https://stackoverflow.com/a/23735006/7396169 – urDMG Mar 03 '18 at 22:07