Skip to content

Commit 85cc8e9

Browse files
committed
Merge pull request #3168 from tomchristie/error-on-erronous-commit-argument
Helpful error on erronous 'serializer.save(commit=False)'
2 parents b226585 + 138e9fc commit 85cc8e9

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

rest_framework/serializers.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,16 @@ def save(self, **kwargs):
153153
'You cannot call `.save()` on a serializer with invalid data.'
154154
)
155155

156+
# Guard against incorrect use of `serializer.save(commit=False)`
157+
assert 'commit' not in kwargs, (
158+
"'commit' is not a valid keyword argument to the 'save()' method. "
159+
"If you need to access data before committing to the database then "
160+
"inspect 'serializer.validated_data' instead. "
161+
"You can also pass additional keyword arguments to 'save()' if you "
162+
"need to set extra attributes on the saved model instance. "
163+
"For example: 'serializer.save(owner=request.user)'.'"
164+
)
165+
156166
validated_data = dict(
157167
list(self.validated_data.items()) +
158168
list(kwargs.items())
@@ -611,6 +621,16 @@ def save(self, **kwargs):
611621
"""
612622
Save and return a list of object instances.
613623
"""
624+
# Guard against incorrect use of `serializer.save(commit=False)`
625+
assert 'commit' not in kwargs, (
626+
"'commit' is not a valid keyword argument to the 'save()' method. "
627+
"If you need to access data before committing to the database then "
628+
"inspect 'serializer.validated_data' instead. "
629+
"You can also pass additional keyword arguments to 'save()' if you "
630+
"need to set extra attributes on the saved model instance. "
631+
"For example: 'serializer.save(owner=request.user)'.'"
632+
)
633+
614634
validated_data = [
615635
dict(list(attrs.items()) + list(kwargs.items()))
616636
for attrs in self.validated_data

0 commit comments

Comments
 (0)