限制 django-rest 框架显示的项目

Restirct items shown by django-restframework

我的 Django 应用程序中有一个书籍模型,每本书都有一个作者。我的模特是

#models.py
class Book(models.Model):
    title = models.CharField()
    author = models.ForeignKey(User, on_delete=models.CASCADE)
class Comment(models.Model):
    book = models.ForeignKey(book)

在我的应用程序中,每个用户都可以对一本书发表评论,每个作者都可以登录并查看评论。我想按作者的书过滤评论,每个作者只能在过滤项中看到自己的书。我怎样才能做到这一点?

您可以通过以下方式检索作者 author 对图书的评论:

comment.objects.filter(<b>book__author=<i>author</i></b>)

author作者的一个User对象。如果 author 写了多本书 he/she 将看到 his/her 本书的所有评论。

Note: The name of a model usually starts with an uppercase, so Book instead of book, and Comment instead of comment.