我想将记录分为两类:

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

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

有帮助吗?

解决方案

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)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top