Commit 7640bfea by paolopaolopaolo

Add `assert` statement to `.save()` method in Serializer:

- Asserts that `_data` does not exist when calling `.save()`
parent 134f5fa4
......@@ -166,6 +166,12 @@ class BaseSerializer(Field):
"For example: 'serializer.save(owner=request.user)'.'"
)
assert not hasattr(self, '_data'), (
"You cannot call `.save()` after accessing `serializer.data`."
"If you need to access data before committing to the database then "
"inspect 'serializer.validated_data' instead. "
)
validated_data = dict(
list(self.validated_data.items()) +
list(kwargs.items())
......
......@@ -61,6 +61,7 @@ class TestSerializer:
with pytest.raises(AssertionError):
serializer.save()
class TestValidateMethod:
def test_non_field_error_validate_method(self):
class ExampleSerializer(serializers.Serializer):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment