Skip to content

Instantly share code, notes, and snippets.

@rca
Created April 17, 2012 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rca/2407724 to your computer and use it in GitHub Desktop.
Save rca/2407724 to your computer and use it in GitHub Desktop.
Adding URI to TastyPie resource
class NotebookResource(BackboneJSResource):
user = fields.ForeignKey(UserResource, 'user')
class Meta:
queryset = Notebook.objects.all()
authentication = MULTI_AUTH
authorization = OwnerAuthorization()
# allow to list posts for this notebook
posts_allowed_methods = ['get', 'post']
filtering = {'id': ALL}
def get_posts(self, request, **kwargs):
kwargs['notebook__id'] = kwargs['pk']
return PostResource().get_list(request, **kwargs)
def post_posts(self, request, **kwargs):
"""
Creates a new post
"""
kwargs['notebook'] = Notebook.objects.get(pk=kwargs.pop('pk'))
return PostResource().post_list(request, **kwargs)
def dispatch_posts(self, request, **kwargs):
"""
A view for handling the various HTTP methods (GET/POST/PUT/DELETE) on
a single resource.
Relies on ``Resource.dispatch`` for the heavy-lifting.
"""
return self.dispatch('posts', request, **kwargs)
def override_urls(self):
return [
url(r"^(?P<resource_name>%s)/(?P<pk>\w[\w/-]*)/posts%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('dispatch_posts'), name="api_dispatch_list_posts"),
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment