Serializer.save() takes keyword arguments, not 'extras' argument

This commit is contained in:
Tom Christie 2014-10-03 13:42:06 +01:00
parent e6c5ebdda6
commit 3a3e2bf57d
3 changed files with 15 additions and 17 deletions

View File

@ -109,16 +109,16 @@ The following example from the tutorial previously used `restore_object()` to ha
This would now be split out into two separate methods.
def update(self, instance, validated_attrs)
instance.title = validated_attrs.get('title', instance.title)
instance.code = validated_attrs.get('code', instance.code)
instance.linenos = validated_attrs.get('linenos', instance.linenos)
instance.language = validated_attrs.get('language', instance.language)
instance.style = validated_attrs.get('style', instance.style)
def update(self, instance, validated_data)
instance.title = validated_data.get('title', instance.title)
instance.code = validated_data.get('code', instance.code)
instance.linenos = validated_data.get('linenos', instance.linenos)
instance.language = validated_data.get('language', instance.language)
instance.style = validated_data.get('style', instance.style)
instance.save()
def create(self, validated_attrs):
return Snippet.objects.create(**validated_attrs)
def create(self, validated_data):
return Snippet.objects.create(**validated_data)
Note that the `.create` method should return the newly created object instance.
@ -134,15 +134,14 @@ For example the following code *is no longer valid*:
serializer.object.user = request.user # Include the user when saving.
serializer.save()
Instead of using `.object` to inspect a partially constructed instance, you would now use `.validated_data` to inspect the cleaned incoming values. Also you can't set extra attributes on the instance directly, but instead pass them to the `.save()` method using the `extras` keyword argument.
Instead of using `.object` to inspect a partially constructed instance, you would now use `.validated_data` to inspect the cleaned incoming values. Also you can't set extra attributes on the instance directly, but instead pass them to the `.save()` method as keyword arguments.
The corresponding code would now look like this:
if serializer.is_valid():
name = serializer.validated_data['name'] # Inspect validated field data.
logging.info('Creating ticket "%s"' % name)
extras = {'user': request.user} # Include the user when saving.
serializer.save(extras=extras)
serializer.save(user=request.user) # Include the user when saving.
#### Limitations of ModelSerializer validation.

View File

@ -101,8 +101,8 @@ class AllowPUTAsCreateMixin(object):
if instance is None:
lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
lookup_value = self.kwargs[lookup_url_kwarg]
extras = {self.lookup_field: lookup_value}
serializer.save(extras=extras)
extra_kwargs = {self.lookup_field: lookup_value}
serializer.save(**extra_kwargs)
return Response(serializer.data, status=status.HTTP_201_CREATED)
serializer.save()

View File

@ -74,12 +74,12 @@ class BaseSerializer(Field):
def create(self, validated_data):
raise NotImplementedError('`create()` must be implemented.')
def save(self, extras=None):
def save(self, **kwargs):
validated_data = self.validated_data
if extras is not None:
if kwargs:
validated_data = dict(
list(validated_data.items()) +
list(extras.items())
list(kwargs.items())
)
if self.instance is not None:
@ -256,7 +256,6 @@ class Serializer(BaseSerializer):
for field_name, field in self.fields.items()
if field.get_value(self._initial_data) is not empty
], serializer=self)
#return self.to_representation(self._initial_data)
return ReturnDict([
(field.field_name, field.get_initial())