如何在Tastypie中设计资源

时间:2014-03-31 09:43:34

标签: django resources tastypie

我在设计博客和评论资源方面存在问题。

class Comment(models.Model):
    publisher = models.ForeignKey(User)
    blog     = models.ForeignKey(Blog)
    publish_date =  models.DateTimeField(auto_now_add=True)
    text =  models.TextField(blank=True, null=True)
    def __unicode__(self):
        return self.text

然后在资源中,我应该如何为BlogResource添加它?

class BlogResource(ModelResource):
    comments = fields.ToManyField('mytest.CommentResource', 'comment_set', readonly=True,full=True,null=True,  blank=True)    

class CommentResource(ModelResource):
    submitter = fields.ToOneField(UserResource, 'submitter', full=True)
    blog = fields.ToOneField(BlogResource, 'blog', full=True)
    class Meta:
        fields = ['text']
        queryset = Comment.objects.all()
        resource_name = 'comments'
        include_resource_uri = False
        authorization = DjangoAuthorization()

要求是: 1.当我们打开博客时,应列出有关博客的所有评论。那就是需要GET操作。 2.删除博客时,应删除所有相关评论。 3.当我们发布博客评论时,它会自动添加到该博客中。

但它在操作中总是存在一些问题。

任何人都可以帮助我吗? 非常感谢!

0 个答案:

没有答案