Merge pull request #302 from rdobson/restframework2

Make default field check safe for boolean values
This commit is contained in:
Tom Christie 2012-10-17 13:55:30 -07:00
commit baf0e366ba
3 changed files with 23 additions and 2 deletions

View File

@ -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

View File

@ -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)

View File

@ -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(
@ -81,7 +85,9 @@ class ValidationTests(TestCase):
'email': 'tom@example.com',
'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):