def test_mixed_choices(self): """ Make sure mixed values for choices works as expected. """ f = serializers.ChoiceField(choices=self.MIXED_CHOICES) value = self.MIXED_CHOICES[1] try: f.to_internal_value(value) except serializers.ValidationError: self.fail("Value %s does not validate" % str(value))
def test_nested_choices(self): """ Make sure a nested value for choices works as expected. """ f = serializers.ChoiceField(choices=self.CHOICES_NESTED) value = self.CHOICES_NESTED[0][1][0][0] try: f.to_internal_value(value) except serializers.ValidationError: self.fail("Value %s does not validate" % str(value))
def test_single_choices(self): """ Make sure a single value for choices works as expected. """ f = serializers.ChoiceField(choices=self.SINGLE_CHOICES) value = self.SINGLE_CHOICES[0] try: f.to_internal_value(value) except serializers.ValidationError: self.fail("Value %s does not validate" % str(value))
class ExampleSerializer(serializers.Serializer): choice_field = serializers.ChoiceField(['red', 'green', 'blue']) integer_field = serializers.IntegerField(min_value=1, max_value=1000) char_field = serializers.CharField(required=False, min_length=3, max_length=40) list_field = serializers.ListField(child=serializers.ListField( child=serializers.IntegerField())) nested_field = NestedField() uuid_field = serializers.UUIDField(label="UUID field")
class ExampleSerializer(serializers.Serializer): choice_field = serializers.ChoiceField(['red', 'green', 'blue']) integer_field = serializers.IntegerField(max_value=10) char_field = serializers.CharField(required=False)
class TestSerializer(serializers.Serializer): test_field = serializers.ChoiceField(choices=choices, initial=2)