Commits

Anonymous committed 9f854ba

Fix kwargs in url conf to be more consistent, also fixes broken blog posts example (every blog post showed all comments)

Comments (0)

Files changed (3)

djangorestframework/modelresource.py

 from django.forms import ModelForm
+from django.db.models import Model
 from django.db.models.query import QuerySet
-from django.db.models import Model
+from django.db.models.fields.related import RelatedField
 
 from djangorestframework.response import Response, ResponseException
 from djangorestframework.resource import Resource
 
     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)
             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:
 
     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):
 
     def get(self, request, auth, *args, **kwargs):
         queryset = self.queryset if self.queryset else self.model.objects.all()
-        return queryset
+        return queryset.filer(**kwargs)
 

examples/blogpost/models.py

     @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

examples/blogpost/urls.py

 urlpatterns = patterns('',
     url(r'^$', BlogPosts.as_view(), name='blog-posts'),
     url(r'^(?P<key>[^/]+)/$', BlogPostInstance.as_view(), name='blog-post'),
-    url(r'^(?P<blogpost_id>[^/]+)/comments/$', Comments.as_view(), name='comments'),
+    url(r'^(?P<blogpost>[^/]+)/comments/$', Comments.as_view(), name='comments'),
     url(r'^(?P<blogpost>[^/]+)/comments/(?P<id>[^/]+)/$', CommentInstance.as_view(), name='comment'),
 )