reverse lookup by ForeignKey in Tastypie

痞子三分冷 提交于 2019-12-02 14:54:58

问题


I have a post model and a comment model that holds the comments that were made for a particular post.

class Post(models.Model):
    body = models.TextField()
    user = models.ForeignKey(User)

class Comment(models.Model):
    post = models.ForeignKey(Post)
    date = models.DateTimeField(auto_now_add=True)
    comment = models.TextField()
    comment_user = models.ForeignKey(User)

Now I want my Post resource to include URI to all the comments attached to a particular post.

I do know I can use fields.ForeignKey to represent which post my comments belong to, but I want the API to have the URI of all the comments that belong to the post in the post object. I hope that makes sense.


回答1:


class PostResource(ModelResource):
     comments = fields.ToManyField(CommentResource, 'comments')

I had been answered similar question before. Check this link



来源:https://stackoverflow.com/questions/14838828/reverse-lookup-by-foreignkey-in-tastypie

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!