Skip to content

Commit

Permalink
Add assert statement to .save() method in Serializer:
Browse files Browse the repository at this point in the history
- Asserts that `_data` does not exist when calling `.save()`
  • Loading branch information
paolopaolopaolo committed Sep 22, 2015
1 parent 134f5fa commit 7640bfe
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
6 changes: 6 additions & 0 deletions rest_framework/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ def save(self, **kwargs):
"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())
Expand Down
1 change: 1 addition & 0 deletions tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def create(validated_data):
with pytest.raises(AssertionError):
serializer.save()


class TestValidateMethod:
def test_non_field_error_validate_method(self):
class ExampleSerializer(serializers.Serializer):
Expand Down

0 comments on commit 7640bfe

Please sign in to comment.