mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-04-02 00:04:30 +03:00
Add assert
statement to .save()
method in Serializer:
- Asserts that `_data` does not exist when calling `.save()`
This commit is contained in:
parent
134f5fa4bc
commit
7640bfea9e
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue
Block a user