From 344db0d733eed8d3be84222a549a651acd6f3ee8 Mon Sep 17 00:00:00 2001 From: Craig Blaszczyk Date: Fri, 8 Jul 2011 18:04:22 +0100 Subject: [PATCH] update mixin to work with m2m data using a through field, by storing the name of the field and manually creating an object in the related table --- djangorestframework/mixins.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index 64e541f10..9f65625e7 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -523,7 +523,10 @@ class CreateModelMixin(object): for field in model._meta.many_to_many: if content.has_key(field.name): - m2m_data[field.name] = content[field.name] + m2m_data[field.name] = ( + model._meta.many_to_many[0].m2m_reverse_field_name(), + content[field.name] + ) del content[field.name] all_kw_args = dict(content.items() + kwargs.items()) @@ -535,7 +538,17 @@ class CreateModelMixin(object): instance.save() for fieldname in m2m_data: - getattr(instance, fieldname).add(*m2m_data[fieldname]) + manager = getattr(instance, fieldname) + + if hasattr(manager, 'add'): + manager.add(*m2m_data[fieldname][1]) + else: + data = {} + data[manager.source_field_name] = instance + + for related_item in m2m_data[fieldname][1]: + data[m2m_data[fieldname][0]] = related_item + manager.through(**data).save() headers = {} if hasattr(instance, 'get_absolute_url'):