public
Created

How to make a TastyPie API with nested resources.

  • Download Gist
api.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14
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()
models.py
Python
1 2 3 4 5 6 7
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)
urls.py
Python
1 2 3 4 5 6 7 8
v1_api = Api(api_name='v1')
v1_api.register(ArticleResource())
v1_api.register(CategoryResource())
 
 
urlpatterns = patterns('',
url(r'^api/', include(v1_api.urls)),
)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.