问题
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