【问题标题】:reverse lookup by ForeignKey in Tastypie在 Tastypie 中通过 ForeignKey 反向查找
【发布时间】:2013-02-12 18:02:21
【问题描述】:

我有一个帖子模型和一个评论模型,其中包含为特定帖子制作的 cmets。

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)

现在我希望我的 Post 资源包含附加到特定帖子的所有 cmets 的 URI。

我知道我可以使用fields.ForeignKey 来表示我的 cmets 属于哪个帖子,但我希望 API 具有帖子对象中属于该帖子的所有 cmets 的 URI。我希望这是有道理的。

【问题讨论】:

    标签: tastypie


    【解决方案1】:
    class PostResource(ModelResource):
         comments = fields.ToManyField(CommentResource, 'comments')
    

    我以前曾被回答过类似的问题。检查这个link

    【讨论】:

    • 是的,它工作得很好。想想你 :) 但我最终决定使用 ID 过滤来获取所有具有正常外键关系的 cmets。这也为我提供了分页功能
    • 你能帮我看看this吗?
    猜你喜欢
    • 2011-05-09
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 2013-04-10
    • 1970-01-01
    相关资源
    最近更新 更多