Search code examples
pythondjangotastypie

Resource nesting with tastypie


Trying to do something pretty simple with tastypie, but cannot figure out how. Lets say I have a model Author and a mode Book with a foreign key pointing to author. And I have an Author resource. So, /api/v1/author would get me a list of authors and /api/v1/author/1 would get me details on a particular author. I want /api/v1/author/1/books to get me a list of books for this particular author. How?

Example code:

from django.db import models from tastypie.resource import ModelResource

class Author(models.Model): name = models.CharField(max_length=200)

class Book(models.Model): title = models.CharField(max_length=200) author = models.ForeignKey('Author')

class AuthorResource(ModelResource): queryset = Author.objects.all()


Solution

  • Looks like the recipe for this was actually in the cookbook, but for something so obvious, the implementation is rather awkward.

    http://django-tastypie.readthedocs.org/en/latest/cookbook.html#nested-resources