From fbb74e7a405b5e75b3c19b656a514550566c0d48 Mon Sep 17 00:00:00 2001 From: iorlas Date: Wed, 8 Oct 2014 15:54:31 +0400 Subject: [PATCH] Fixed bug in ChoiceField, when it makes possible to submit 0 as value, when it is not in choices Since we check just for bool(value) and bool(0) is False, it doesn't perform validation even when required=True. required=True logic checks value for existing in django core EMPTY_VALUES list, but 0 is not in it, which is logical. But in validate of ChoiseField, we should do "if value is provided and it is not empty(None, [], {}), perform validation". Tom, it is critical, please, take a look :( --- rest_framework/fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index c0253f86b..7744010d7 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -552,7 +552,7 @@ class ChoiceField(WritableField): Validates that the input is in self.choices. """ super(ChoiceField, self).validate(value) - if value and not self.valid_value(value): + if value not in validators.EMPTY_VALUES and not self.valid_value(value): raise ValidationError(self.error_messages['invalid_choice'] % {'value': value}) def valid_value(self, value):