mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-24 02:24:03 +03:00
Merge pull request #797 from maspwr/writable-nested-modelserializer
Don't use field when saving related data
This commit is contained in:
commit
0080bf8a00
|
@ -460,7 +460,7 @@ class BaseSerializer(WritableField):
|
||||||
ret.append(self.from_native(item, None))
|
ret.append(self.from_native(item, None))
|
||||||
errors.append(self._errors)
|
errors.append(self._errors)
|
||||||
|
|
||||||
if update:
|
if update and self.allow_add_remove:
|
||||||
ret._deleted = identity_to_objects.values()
|
ret._deleted = identity_to_objects.values()
|
||||||
|
|
||||||
self._errors = any(errors) and errors or []
|
self._errors = any(errors) and errors or []
|
||||||
|
@ -514,7 +514,7 @@ class BaseSerializer(WritableField):
|
||||||
if isinstance(self.object, list):
|
if isinstance(self.object, list):
|
||||||
[self.save_object(item, **kwargs) for item in self.object]
|
[self.save_object(item, **kwargs) for item in self.object]
|
||||||
|
|
||||||
if self.allow_add_remove and self.object._deleted:
|
if self.object._deleted:
|
||||||
[self.delete_object(item) for item in self.object._deleted]
|
[self.delete_object(item) for item in self.object._deleted]
|
||||||
else:
|
else:
|
||||||
self.save_object(self.object, **kwargs)
|
self.save_object(self.object, **kwargs)
|
||||||
|
@ -779,9 +779,7 @@ class ModelSerializer(Serializer):
|
||||||
|
|
||||||
if getattr(obj, '_related_data', None):
|
if getattr(obj, '_related_data', None):
|
||||||
for accessor_name, related in obj._related_data.items():
|
for accessor_name, related in obj._related_data.items():
|
||||||
field = self.fields.get(accessor_name, None)
|
if isinstance(related, RelationsList):
|
||||||
if isinstance(field, Serializer):
|
|
||||||
if field.many:
|
|
||||||
# Nested reverse fk relationship
|
# Nested reverse fk relationship
|
||||||
for related_item in related:
|
for related_item in related:
|
||||||
fk_field = obj._meta.get_field_by_name(accessor_name)[0].field.name
|
fk_field = obj._meta.get_field_by_name(accessor_name)[0].field.name
|
||||||
|
@ -789,10 +787,10 @@ class ModelSerializer(Serializer):
|
||||||
self.save_object(related_item)
|
self.save_object(related_item)
|
||||||
|
|
||||||
# Delete any removed objects
|
# Delete any removed objects
|
||||||
if field.allow_add_remove and related._deleted:
|
if related._deleted:
|
||||||
[self.delete_object(item) for item in related._deleted]
|
[self.delete_object(item) for item in related._deleted]
|
||||||
|
|
||||||
else:
|
elif isinstance(related, models.Model):
|
||||||
# Nested reverse one-one relationship
|
# Nested reverse one-one relationship
|
||||||
fk_field = obj._meta.get_field_by_name(accessor_name)[0].field.name
|
fk_field = obj._meta.get_field_by_name(accessor_name)[0].field.name
|
||||||
setattr(related, fk_field, obj)
|
setattr(related, fk_field, obj)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user