mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-23 01:57:00 +03:00
Merge pull request #3168 from tomchristie/error-on-erronous-commit-argument
Helpful error on erronous 'serializer.save(commit=False)'
This commit is contained in:
commit
85cc8e9f1a
|
@ -153,6 +153,16 @@ class BaseSerializer(Field):
|
|||
'You cannot call `.save()` on a serializer with invalid data.'
|
||||
)
|
||||
|
||||
# Guard against incorrect use of `serializer.save(commit=False)`
|
||||
assert 'commit' not in kwargs, (
|
||||
"'commit' is not a valid keyword argument to the 'save()' method. "
|
||||
"If you need to access data before committing to the database then "
|
||||
"inspect 'serializer.validated_data' instead. "
|
||||
"You can also pass additional keyword arguments to 'save()' if you "
|
||||
"need to set extra attributes on the saved model instance. "
|
||||
"For example: 'serializer.save(owner=request.user)'.'"
|
||||
)
|
||||
|
||||
validated_data = dict(
|
||||
list(self.validated_data.items()) +
|
||||
list(kwargs.items())
|
||||
|
@ -611,6 +621,16 @@ class ListSerializer(BaseSerializer):
|
|||
"""
|
||||
Save and return a list of object instances.
|
||||
"""
|
||||
# Guard against incorrect use of `serializer.save(commit=False)`
|
||||
assert 'commit' not in kwargs, (
|
||||
"'commit' is not a valid keyword argument to the 'save()' method. "
|
||||
"If you need to access data before committing to the database then "
|
||||
"inspect 'serializer.validated_data' instead. "
|
||||
"You can also pass additional keyword arguments to 'save()' if you "
|
||||
"need to set extra attributes on the saved model instance. "
|
||||
"For example: 'serializer.save(owner=request.user)'.'"
|
||||
)
|
||||
|
||||
validated_data = [
|
||||
dict(list(attrs.items()) + list(kwargs.items()))
|
||||
for attrs in self.validated_data
|
||||
|
|
Loading…
Reference in New Issue
Block a user