This commit is contained in:
Хасанов Булат 2017-06-07 03:59:17 +00:00 committed by GitHub
commit aaf604c3ac
2 changed files with 20 additions and 2 deletions

View File

@ -910,7 +910,7 @@ class IntegerField(Field):
return data
def to_representation(self, value):
return int(value)
return int(value) if value is not None else None
class FloatField(Field):
@ -944,7 +944,7 @@ class FloatField(Field):
self.fail('invalid')
def to_representation(self, value):
return float(value)
return float(value) if value is not None else None
class DecimalField(Field):

View File

@ -853,6 +853,15 @@ class TestIntegerField(FieldValues):
}
field = serializers.IntegerField()
def test_allow_null(self):
"""
If `allow_null=True` then `None` is a valid input.
"""
field = serializers.IntegerField(allow_null=True)
output = field.run_validation(None)
assert output is None
assert field.to_representation(None) is None
class TestMinMaxIntegerField(FieldValues):
"""
@ -899,6 +908,15 @@ class TestFloatField(FieldValues):
}
field = serializers.FloatField()
def test_allow_null(self):
"""
If `allow_null=True` then `None` is a valid input.
"""
field = serializers.FloatField(allow_null=True)
output = field.run_validation(None)
assert output is None
assert field.to_representation(None) is None
class TestMinMaxFloatField(FieldValues):
"""