Another fix for nested writable serializers in case of the related_name isn’t set on the ForeignKey.

This commit is contained in:
Xavier Ordoquy 2013-11-18 15:06:39 +01:00
parent ad7aa8fe48
commit a8b15f4290

View File

@ -940,11 +940,12 @@ class ModelSerializer(Serializer):
del(obj._m2m_data)
if getattr(obj, '_related_data', None):
related_fields = dict(((f.get_accessor_name(), f) for f, m in obj._meta.get_all_related_objects_with_model()))
for accessor_name, related in obj._related_data.items():
if isinstance(related, RelationsList):
# Nested reverse fk relationship
for related_item in related:
fk_field = obj._meta.get_field_by_name(accessor_name)[0].field.name
fk_field = related_fields[accessor_name].field.name
setattr(related_item, fk_field, obj)
self.save_object(related_item)