mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-02 20:54:42 +03:00
Serializer.save() takes keyword arguments, not 'extras' argument
This commit is contained in:
parent
e6c5ebdda6
commit
3a3e2bf57d
|
@ -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.
|
This would now be split out into two separate methods.
|
||||||
|
|
||||||
def update(self, instance, validated_attrs)
|
def update(self, instance, validated_data)
|
||||||
instance.title = validated_attrs.get('title', instance.title)
|
instance.title = validated_data.get('title', instance.title)
|
||||||
instance.code = validated_attrs.get('code', instance.code)
|
instance.code = validated_data.get('code', instance.code)
|
||||||
instance.linenos = validated_attrs.get('linenos', instance.linenos)
|
instance.linenos = validated_data.get('linenos', instance.linenos)
|
||||||
instance.language = validated_attrs.get('language', instance.language)
|
instance.language = validated_data.get('language', instance.language)
|
||||||
instance.style = validated_attrs.get('style', instance.style)
|
instance.style = validated_data.get('style', instance.style)
|
||||||
instance.save()
|
instance.save()
|
||||||
|
|
||||||
def create(self, validated_attrs):
|
def create(self, validated_data):
|
||||||
return Snippet.objects.create(**validated_attrs)
|
return Snippet.objects.create(**validated_data)
|
||||||
|
|
||||||
Note that the `.create` method should return the newly created object instance.
|
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.object.user = request.user # Include the user when saving.
|
||||||
serializer.save()
|
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:
|
The corresponding code would now look like this:
|
||||||
|
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
name = serializer.validated_data['name'] # Inspect validated field data.
|
name = serializer.validated_data['name'] # Inspect validated field data.
|
||||||
logging.info('Creating ticket "%s"' % name)
|
logging.info('Creating ticket "%s"' % name)
|
||||||
extras = {'user': request.user} # Include the user when saving.
|
serializer.save(user=request.user) # Include the user when saving.
|
||||||
serializer.save(extras=extras)
|
|
||||||
|
|
||||||
#### Limitations of ModelSerializer validation.
|
#### Limitations of ModelSerializer validation.
|
||||||
|
|
||||||
|
|
|
@ -101,8 +101,8 @@ class AllowPUTAsCreateMixin(object):
|
||||||
if instance is None:
|
if instance is None:
|
||||||
lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
|
lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
|
||||||
lookup_value = self.kwargs[lookup_url_kwarg]
|
lookup_value = self.kwargs[lookup_url_kwarg]
|
||||||
extras = {self.lookup_field: lookup_value}
|
extra_kwargs = {self.lookup_field: lookup_value}
|
||||||
serializer.save(extras=extras)
|
serializer.save(**extra_kwargs)
|
||||||
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||||
|
|
||||||
serializer.save()
|
serializer.save()
|
||||||
|
|
|
@ -74,12 +74,12 @@ class BaseSerializer(Field):
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
raise NotImplementedError('`create()` must be implemented.')
|
raise NotImplementedError('`create()` must be implemented.')
|
||||||
|
|
||||||
def save(self, extras=None):
|
def save(self, **kwargs):
|
||||||
validated_data = self.validated_data
|
validated_data = self.validated_data
|
||||||
if extras is not None:
|
if kwargs:
|
||||||
validated_data = dict(
|
validated_data = dict(
|
||||||
list(validated_data.items()) +
|
list(validated_data.items()) +
|
||||||
list(extras.items())
|
list(kwargs.items())
|
||||||
)
|
)
|
||||||
|
|
||||||
if self.instance is not None:
|
if self.instance is not None:
|
||||||
|
@ -256,7 +256,6 @@ class Serializer(BaseSerializer):
|
||||||
for field_name, field in self.fields.items()
|
for field_name, field in self.fields.items()
|
||||||
if field.get_value(self._initial_data) is not empty
|
if field.get_value(self._initial_data) is not empty
|
||||||
], serializer=self)
|
], serializer=self)
|
||||||
#return self.to_representation(self._initial_data)
|
|
||||||
|
|
||||||
return ReturnDict([
|
return ReturnDict([
|
||||||
(field.field_name, field.get_initial())
|
(field.field_name, field.get_initial())
|
||||||
|
|
Loading…
Reference in New Issue
Block a user