diff --git a/.gitignore b/.gitignore index 0064fb0..ba82493 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,5 @@ docs/_build/ # Vim swap files *.swp +/venv/ +/.idea/ diff --git a/serpy/serializer.py b/serpy/serializer.py index 06b5bf6..c4d6275 100644 --- a/serpy/serializer.py +++ b/serpy/serializer.py @@ -115,6 +115,9 @@ def _serialize(self, instance, fields): raise else: continue + if not required and not result: + continue + if required or result is not None: if call: result = result() diff --git a/tests/test_serializer.py b/tests/test_serializer.py index 1947a0b..fa6ab0a 100644 --- a/tests/test_serializer.py +++ b/tests/test_serializer.py @@ -158,7 +158,7 @@ class ASerializer(Serializer): o = Obj(a=None) data = ASerializer(o).data - self.assertIsNone(data['a']) + self.assertNotIn('a', data) o = Obj(a='5') data = ASerializer(o).data @@ -176,9 +176,6 @@ class ASerializer(DictSerializer): a = Field(required=False) data = ASerializer({'a': None}).data - self.assertIsNone(data['a']) - - data = ASerializer({}).data self.assertNotIn('a', data) class ASerializer(DictSerializer): @@ -196,10 +193,6 @@ class ASerializer(Serializer): o = Obj(a=None) data = ASerializer(o).data - self.assertIsNone(data['a']) - - o = Obj() - data = ASerializer(o).data self.assertNotIn('a', data) class ASerializer(Serializer):