【问题标题】:Count records having three or more items计算具有三个或更多项目的记录
【发布时间】:2009-12-04 05:54:47
【问题描述】:

我想将记录分为两类:

  1. 具有三个或更多记录的项目
  2. 少于三个项目的项目

我该怎么做?我正在考虑使用 annotate()。

【问题讨论】:

  • 请提供更多细节。很难理解你想做什么
  • 请展示您的模型以及结果应该是什么样子。

标签: django annotate summarization


【解决方案1】:
q = Book.objects.annotate(num_authors=Count('authors'))
books_with_3_or_over_authors = q.filter(num_authors__gte=3)
books_with_less_than_3_authors = q.filter(num_authors__lt=3)

【讨论】:

    猜你喜欢
    • 2017-04-30
    • 2019-06-11
    • 1970-01-01
    • 2016-06-29
    • 2011-10-07
    • 2023-04-01
    • 1970-01-01
    • 2012-01-26
    • 2021-01-05
    相关资源
    最近更新 更多