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)