Merge pull request #336 from markotibold/null_is_true_model_fields

Null is true model fields
This commit is contained in:
Tom Christie 2012-10-30 02:22:51 -07:00
commit e5777a6168
3 changed files with 21 additions and 0 deletions

View File

@ -393,6 +393,9 @@ class ModelSerializer(Serializer):
Creates a default instance of a basic non-relational field.
"""
kwargs = {}
if model_field.null:
kwargs['required'] = False
if model_field.has_default():
kwargs['required'] = False
kwargs['default'] = model_field.get_default()

View File

@ -116,3 +116,8 @@ class BlogPost(RESTFrameworkModel):
class BlogPostComment(RESTFrameworkModel):
text = models.TextField()
blog_post = models.ForeignKey(BlogPost)
class Person(RESTFrameworkModel):
name = models.CharField(max_length=10)
age = models.IntegerField(null=True, blank=True)

View File

@ -43,6 +43,11 @@ class ActionItemSerializer(serializers.ModelSerializer):
model = ActionItem
class PersonSerializer(serializers.ModelSerializer):
class Meta:
model = Person
class BasicTests(TestCase):
def setUp(self):
self.comment = Comment(
@ -188,6 +193,14 @@ class ValidationTests(TestCase):
self.assertFalse(serializer.is_valid())
self.assertEquals(serializer.errors, {'non_field_errors': [u'Email address not in content']})
def test_null_is_true_fields(self):
"""
Omitting a value for null-field should validate.
"""
serializer = PersonSerializer({'name': 'marko'})
self.assertEquals(serializer.is_valid(), True)
self.assertEquals(serializer.errors, {})
class MetadataTests(TestCase):
def test_empty(self):