Skip to content

Commit 79e18a2

Browse files
committed
Raise assertion error if calling .save() on a serializer with errors. Closes #2098.
1 parent 6ac79b8 commit 79e18a2

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

rest_framework/serializers.py

+8
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ def save(self, **kwargs):
127127
(self.__class__.__module__, self.__class__.__name__)
128128
)
129129

130+
assert hasattr(self, '_errors'), (
131+
'You must call `.is_valid()` before calling `.save()`.'
132+
)
133+
134+
assert not self.errors, (
135+
'You cannot call `.save()` on a serializer with invalid data.'
136+
)
137+
130138
validated_data = dict(
131139
list(self.validated_data.items()) +
132140
list(kwargs.items())

0 commit comments

Comments
 (0)