mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-27 03:54:01 +03:00
Merge pull request #1291 from kevin-brown/issue_1101
Fix default values always being False for browsable API
This commit is contained in:
commit
8b2f0cc3d7
|
@ -428,7 +428,7 @@ class BooleanField(WritableField):
|
|||
def field_from_native(self, data, files, field_name, into):
|
||||
# HTML checkboxes do not explicitly represent unchecked as `False`
|
||||
# we deal with that here...
|
||||
if isinstance(data, QueryDict):
|
||||
if isinstance(data, QueryDict) and self.default is None:
|
||||
self.default = False
|
||||
|
||||
return super(BooleanField, self).field_from_native(
|
||||
|
|
|
@ -1743,3 +1743,42 @@ class TestSerializerTransformMethods(TestCase):
|
|||
'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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user