Merge pull request #1291 from kevin-brown/issue_1101

Fix default values always being False for browsable API
This commit is contained in:
Tom Christie 2013-12-13 12:15:23 -08:00
commit 8b2f0cc3d7
2 changed files with 40 additions and 1 deletions

View File

@ -428,7 +428,7 @@ class BooleanField(WritableField):
def field_from_native(self, data, files, field_name, into): def field_from_native(self, data, files, field_name, into):
# HTML checkboxes do not explicitly represent unchecked as `False` # HTML checkboxes do not explicitly represent unchecked as `False`
# we deal with that here... # we deal with that here...
if isinstance(data, QueryDict): if isinstance(data, QueryDict) and self.default is None:
self.default = False self.default = False
return super(BooleanField, self).field_from_native( return super(BooleanField, self).field_from_native(

View File

@ -1743,3 +1743,42 @@ class TestSerializerTransformMethods(TestCase):
'b_renamed': None, 'b_renamed': None,
} }
) )
class DefaultTrueBooleanModel(models.Model):
cat = models.BooleanField(default=True)
dog = models.BooleanField(default=False)
class SerializerDefaultTrueBoolean(TestCase):
def setUp(self):
super(SerializerDefaultTrueBoolean, self).setUp()
class DefaultTrueBooleanSerializer(serializers.ModelSerializer):
class Meta:
model = DefaultTrueBooleanModel
fields = ('cat', 'dog')
self.default_true_boolean_serializer = DefaultTrueBooleanSerializer
def test_enabled_as_false(self):
serializer = self.default_true_boolean_serializer(data={'cat': False,
'dog': False})
self.assertEqual(serializer.is_valid(), True)
self.assertEqual(serializer.data['cat'], False)
self.assertEqual(serializer.data['dog'], False)
def test_enabled_as_true(self):
serializer = self.default_true_boolean_serializer(data={'cat': True,
'dog': True})
self.assertEqual(serializer.is_valid(), True)
self.assertEqual(serializer.data['cat'], True)
self.assertEqual(serializer.data['dog'], True)
def test_enabled_partial(self):
serializer = self.default_true_boolean_serializer(data={'cat': False},
partial=True)
self.assertEqual(serializer.is_valid(), True)
self.assertEqual(serializer.data['cat'], False)
self.assertEqual(serializer.data['dog'], False)