mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-23 15:54:16 +03:00
Merge pull request #336 from markotibold/null_is_true_model_fields
Null is true model fields
This commit is contained in:
commit
e5777a6168
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue
Block a user