class MultipleChoiceField(serializers.WritableField):
"""
A field that behaves like multiple choice field of Django forms.
"""
def from_native(self, data):
if isinstance(data, list):
for item in data:
if not item in self.choices:
raise serializers.ValidationError("The item you entered is not in the allowed items list.")
return data
else:
raise serializers.ValidationError("Please provide a valid list.")
def to_native(self, value):
return value
def __init__(self, choices=None, *args, **kwargs):
self.choices = dict(choices)
super(MultipleChoiceField, self).__init__(*args, **kwargs)
But i still can't believe that there is no multiple choice field on DRF. This must be on purpose. I am very curious about it :)
13 Mart 2014 Perşembe 16:42:47 UTC+2 tarihinde Yigit Guler yazdı: