diff options
| author | GitHub Merge Button | 2011-07-11 12:33:31 -0700 |
|---|---|---|
| committer | GitHub Merge Button | 2011-07-11 12:33:31 -0700 |
| commit | 7f0b291528061accf921e58253fd38b018d427ca (patch) | |
| tree | b28bfb1cea09f7bc2851a4fc2d28ee4a60a50859 /djangorestframework/mixins.py | |
| parent | e4ce57b88cdcb3a0e642d05a74b392c3303929fc (diff) | |
| parent | 8a2944acdf66a966f0bd4ea4770f012f2597fb4e (diff) | |
| download | django-rest-framework-7f0b291528061accf921e58253fd38b018d427ca.tar.bz2 | |
Merge 8a2944acdf66a966f0bd4ea4770f012f2597fb4e into e4ce57b88cdcb3a0e642d05a74b392c3303929fc
Diffstat (limited to 'djangorestframework/mixins.py')
| -rw-r--r-- | djangorestframework/mixins.py | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index 5cec2580..4d8b2d35 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -5,7 +5,7 @@ classes that can be added to a `View`. from django.contrib.auth.models import AnonymousUser from django.db.models.query import QuerySet -from django.db.models.fields.related import RelatedField +from django.db.models.fields.related import ForeignKey from django.http import HttpResponse from django.http.multipartparser import LimitBytes @@ -509,21 +509,35 @@ class CreateModelMixin(object): """ Behavior to create a `model` instance on POST requests """ - def post(self, request, *args, **kwargs): + def post(self, request, *args, **kwargs): model = self.resource.model - # translated 'related_field' kwargs into 'related_field_id' - for related_name in [field.name for field in model._meta.fields if isinstance(field, RelatedField)]: - if kwargs.has_key(related_name): - kwargs[related_name + '_id'] = kwargs[related_name] - del kwargs[related_name] + # Copy the dict to keep self.CONTENT intact + content = dict(self.CONTENT) + m2m_data = {} + + for field in model._meta.fields: + if isinstance(field, ForeignKey) and kwargs.has_key(field.name): + # translate 'related_field' kwargs into 'related_field_id' + kwargs[field.name + '_id'] = kwargs[field.name] + del kwargs[field.name] + + for field in model._meta.many_to_many: + if content.has_key(field.name): + m2m_data[field.name] = content[field.name] + del content[field.name] + + all_kw_args = dict(content.items() + kwargs.items()) - all_kw_args = dict(self.CONTENT.items() + kwargs.items()) if args: instance = model(pk=args[-1], **all_kw_args) else: instance = model(**all_kw_args) instance.save() + + for fieldname in m2m_data: + getattr(instance, fieldname).add(*m2m_data[fieldname]) + headers = {} if hasattr(instance, 'get_absolute_url'): headers['Location'] = self.resource(self).url(instance) |
