Created
January 25, 2012 16:04
-
-
Save asmallteapot/1676959 to your computer and use it in GitHub Desktop.
How to make a TastyPie API with nested resources.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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