From 138e9fcc8ff07e7717df0824140ead2b1bddb66e Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 17 Jul 2015 12:39:22 +0100 Subject: [PATCH] Helpful error on erronous 'serializer.save(commit=False)' --- rest_framework/serializers.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 4562329ee..fa3a53374 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -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