mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 03:23:59 +03:00
Merge pull request #302 from rdobson/restframework2
Make default field check safe for boolean values
This commit is contained in:
commit
baf0e366ba
|
@ -393,7 +393,7 @@ class ModelSerializer(Serializer):
|
|||
except KeyError:
|
||||
ret = ModelField(model_field=model_field)
|
||||
|
||||
if model_field.default:
|
||||
if model_field.default is not None:
|
||||
ret.required = False
|
||||
|
||||
return ret
|
||||
|
|
|
@ -91,3 +91,7 @@ class Comment(RESTFrameworkModel):
|
|||
email = models.EmailField()
|
||||
content = models.CharField(max_length=200)
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
class ActionItem(RESTFrameworkModel):
|
||||
title = models.CharField(max_length=200)
|
||||
done = models.BooleanField(default=False)
|
||||
|
|
|
@ -28,6 +28,10 @@ class CommentSerializer(serializers.Serializer):
|
|||
return instance
|
||||
|
||||
|
||||
class ActionItemSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = ActionItem
|
||||
|
||||
class BasicTests(TestCase):
|
||||
def setUp(self):
|
||||
self.comment = Comment(
|
||||
|
@ -82,6 +86,8 @@ class ValidationTests(TestCase):
|
|||
'content': 'x' * 1001,
|
||||
'created': datetime.datetime(2012, 1, 1)
|
||||
}
|
||||
self.actionitem = ActionItem('Some to do item',
|
||||
)
|
||||
|
||||
def test_create(self):
|
||||
serializer = CommentSerializer(self.data)
|
||||
|
@ -102,6 +108,17 @@ class ValidationTests(TestCase):
|
|||
self.assertEquals(serializer.is_valid(), False)
|
||||
self.assertEquals(serializer.errors, {'email': [u'This field is required.']})
|
||||
|
||||
def test_missing_bool_with_default(self):
|
||||
"""Make sure that a boolean value with a 'False' value is not
|
||||
mistaken for not having a default."""
|
||||
data = {
|
||||
'title':'Some action item',
|
||||
#No 'done' value.
|
||||
}
|
||||
serializer = ActionItemSerializer(data, instance=self.actionitem)
|
||||
self.assertEquals(serializer.is_valid(), True)
|
||||
self.assertEquals(serializer.errors, {})
|
||||
|
||||
|
||||
class MetadataTests(TestCase):
|
||||
def test_empty(self):
|
||||
|
|
Loading…
Reference in New Issue
Block a user