diff --git a/djangorestframework/modelresource.py b/djangorestframework/modelresource.py index 0123e77af..0b06dacb7 100644 --- a/djangorestframework/modelresource.py +++ b/djangorestframework/modelresource.py @@ -1,6 +1,7 @@ from django.forms import ModelForm -from django.db.models.query import QuerySet from django.db.models import Model +from django.db.models.query import QuerySet +from django.db.models.fields.related import RelatedField from djangorestframework.response import Response, ResponseException from djangorestframework.resource import Resource @@ -340,6 +341,13 @@ class ModelResource(Resource, ModelFormValidatorMixin): def post(self, request, auth, content, *args, **kwargs): # TODO: test creation on a non-existing resource url + + # translated related_field into related_field_id + for related_name in [field.name for field in self.model._meta.fields if isinstance(field, RelatedField)]: + if kwargs.has_key(related_name): + kwargs[related_name + '_id'] = kwargs[related_name] + del kwargs[related_name] + all_kw_args = dict(content.items() + kwargs.items()) if args: instance = self.model(pk=args[-1], **all_kw_args) @@ -373,6 +381,7 @@ class ModelResource(Resource, ModelFormValidatorMixin): else: # Otherwise assume the kwargs uniquely identify the model instance = self.model.objects.get(**kwargs) + for (key, val) in content.items(): setattr(instance, key, val) except self.model.DoesNotExist: @@ -404,7 +413,7 @@ class RootModelResource(ModelResource): def get(self, request, auth, *args, **kwargs): queryset = self.queryset if self.queryset else self.model.objects.all() - return queryset + return queryset.filter(**kwargs) class QueryModelResource(ModelResource): @@ -418,5 +427,5 @@ class QueryModelResource(ModelResource): def get(self, request, auth, *args, **kwargs): queryset = self.queryset if self.queryset else self.model.objects.all() - return queryset + return queryset.filer(**kwargs) diff --git a/examples/blogpost/models.py b/examples/blogpost/models.py index c85ca7882..ba7810dc6 100644 --- a/examples/blogpost/models.py +++ b/examples/blogpost/models.py @@ -30,7 +30,7 @@ class BlogPost(models.Model): @models.permalink def comments_url(self): """Link to a resource which lists all comments for this blog post.""" - return ('comments', (), {'blogpost_id': self.key}) + return ('comments', (), {'blogpost': self.key}) def __unicode__(self): return self.title diff --git a/examples/blogpost/urls.py b/examples/blogpost/urls.py index b2df96a17..1306b0d7d 100644 --- a/examples/blogpost/urls.py +++ b/examples/blogpost/urls.py @@ -4,6 +4,6 @@ from blogpost.views import BlogPosts, BlogPostInstance, Comments, CommentInstanc urlpatterns = patterns('', url(r'^$', BlogPosts.as_view(), name='blog-posts'), url(r'^(?P[^/]+)/$', BlogPostInstance.as_view(), name='blog-post'), - url(r'^(?P[^/]+)/comments/$', Comments.as_view(), name='comments'), + url(r'^(?P[^/]+)/comments/$', Comments.as_view(), name='comments'), url(r'^(?P[^/]+)/comments/(?P[^/]+)/$', CommentInstance.as_view(), name='comment'), )