mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-27 20:14:01 +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,24 +779,22 @@ 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):
|
# Nested reverse fk relationship
|
||||||
if field.many:
|
for related_item in related:
|
||||||
# Nested reverse fk relationship
|
|
||||||
for related_item in related:
|
|
||||||
fk_field = obj._meta.get_field_by_name(accessor_name)[0].field.name
|
|
||||||
setattr(related_item, fk_field, obj)
|
|
||||||
self.save_object(related_item)
|
|
||||||
|
|
||||||
# Delete any removed objects
|
|
||||||
if field.allow_add_remove and related._deleted:
|
|
||||||
[self.delete_object(item) for item in related._deleted]
|
|
||||||
|
|
||||||
else:
|
|
||||||
# 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_item, fk_field, obj)
|
||||||
self.save_object(related)
|
self.save_object(related_item)
|
||||||
|
|
||||||
|
# Delete any removed objects
|
||||||
|
if related._deleted:
|
||||||
|
[self.delete_object(item) for item in related._deleted]
|
||||||
|
|
||||||
|
elif isinstance(related, models.Model):
|
||||||
|
# Nested reverse one-one relationship
|
||||||
|
fk_field = obj._meta.get_field_by_name(accessor_name)[0].field.name
|
||||||
|
setattr(related, fk_field, obj)
|
||||||
|
self.save_object(related)
|
||||||
else:
|
else:
|
||||||
# Reverse FK or reverse one-one
|
# Reverse FK or reverse one-one
|
||||||
setattr(obj, accessor_name, related)
|
setattr(obj, accessor_name, related)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user