django-rest-framework/examples/blogpost/resources.py
Daniel Izquierdo c7e7279d97 Update examples to use the new custom `reverse()'
This fixes #167 except for the docs
2012-02-21 22:51:32 +09:00

28 lines
951 B
Python

from djangorestframework.resources import ModelResource
from djangorestframework.utils import reverse
from blogpost.models import BlogPost, Comment
class BlogPostResource(ModelResource):
"""
A Blog Post has a *title* and *content*, and can be associated with zero or more comments.
"""
model = BlogPost
fields = ('created', 'title', 'slug', 'content', 'url', 'comments')
ordering = ('-created',)
def comments(self, instance):
return reverse('comments', request, kwargs={'blogpost': instance.key})
class CommentResource(ModelResource):
"""
A Comment is associated with a given Blog Post and has a *username* and *comment*, and optionally a *rating*.
"""
model = Comment
fields = ('username', 'comment', 'created', 'rating', 'url', 'blogpost')
ordering = ('-created',)
def blogpost(self, instance):
return reverse('blog-post', request, kwargs={'key': instance.blogpost.key})