Skip to content

Instantly share code, notes, and snippets.

@asmallteapot
Created January 25, 2012 16:04
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 asmallteapot/1676959 to your computer and use it in GitHub Desktop.
Save asmallteapot/1676959 to your computer and use it in GitHub Desktop.
How to make a TastyPie API with nested resources.
class ArticleResource(ModelResource):
class Meta:
authentication = Authentication()
authorization = DjangoAuthorization()
queryset = Article.objects.all()
class CategoryResource(ModelResource):
articles = fields.ToManyField('locations.api.Articles', 'articles', full=True)
class Meta:
authentication = Authentication()
authorization = DjangoAuthorization()
queryset = Category.objects.all()
class Article(models.Model):
category = models.ForeignKey('Category')
title = models.CharField(max_length=50)
content = models.TextField()
class Category(models.Model):
name = models.CharField(max_length=50)
v1_api = Api(api_name='v1')
v1_api.register(ArticleResource())
v1_api.register(CategoryResource())
urlpatterns = patterns('',
url(r'^api/', include(v1_api.urls)),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment