django 中过滤图书列表的每位作者的图书数量

发布于 2024-11-05 22:02:47 字数 475 浏览 0 评论 0原文

简短的问题。 我有两个模型:

class Author(models.Model):
    name = models.CharField(max_length=250)

class Book(models.Model):
    title = models.CharField(max_length=250)
    author = models.ManyToManyField(Author)

一个视图:

def filter_books(request):
    book_list = Book.objects.filter(...)

如何在模板中显示下一个内容:

Authors in selected books:
    Author1: book_count
    Author2: book_count
    ...

Short question.
I have two models:

class Author(models.Model):
    name = models.CharField(max_length=250)

class Book(models.Model):
    title = models.CharField(max_length=250)
    author = models.ManyToManyField(Author)

One view:

def filter_books(request):
    book_list = Book.objects.filter(...)

How can I display in template next content:

Authors in selected books:
    Author1: book_count
    Author2: book_count
    ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云裳 2024-11-12 22:02:47

让我们逐步构建查询。

首先,获取在 book_list 中拥有书籍的作者。

authors = Author.objects.filter(book__in=book_list)

诀窍是要意识到 book_list 中每本书的作者都会出现一次。然后我们可以使用 annotate 来统计作者出现的次数。

# remember to import Count!
from django.db.models import Count

authors = Author.objects.filter(book__in=book_list
          ).annotate(num_books=Count('id')

在模板中,您可以执行以下操作:

Authors in selected books:
{% for author in authors %}
{{ author.name }}: {{ author.num_books }}<br />
{% endfor %}

Let's build the query step by step.

First, get the authors who have a book in book_list.

authors = Author.objects.filter(book__in=book_list)

The trick is to realise that an author will appear once for each book in book_list. We can then use annotate to count the number of times the author appears.

# remember to import Count!
from django.db.models import Count

authors = Author.objects.filter(book__in=book_list
          ).annotate(num_books=Count('id')

In the template, you can then do:

Authors in selected books:
{% for author in authors %}
{{ author.name }}: {{ author.num_books }}<br />
{% endfor %}
风铃鹿 2024-11-12 22:02:47
#first way
class Author(models.Model):
    name = models.CharField(max_length=250)

class Book(models.Model):
    title = models.CharField(max_length=250)
    author = models.ManyToManyField(Author, related_name='bookauthor_sets')

直接从模板使用它:

{{ author.bookauthor_sets.all.count }}

意味着

author.bookauthor_sets.all().count() #with python

您也可以这样做:

#second one
class Author(models.Model):
    name = models.CharField(max_length=250)

    def get_books_count(self):
        return Book.objects.filter(author=self).count()

在模板中:

{{ author.get_books_count }}

我希望这些信息应该有所帮助。

#first way
class Author(models.Model):
    name = models.CharField(max_length=250)

class Book(models.Model):
    title = models.CharField(max_length=250)
    author = models.ManyToManyField(Author, related_name='bookauthor_sets')

Use it directly from template:

{{ author.bookauthor_sets.all.count }}

means

author.bookauthor_sets.all().count() #with python

You can also do like this:

#second one
class Author(models.Model):
    name = models.CharField(max_length=250)

    def get_books_count(self):
        return Book.objects.filter(author=self).count()

In the template:

{{ author.get_books_count }}

I hope this information should help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文