2009-12-04 49 views
0

我想組記錄分爲兩類:具有三個以上的記錄盤點記錄

  1. 項目
  2. 項目具有小於三個項目

怎麼辦我去解決這個問題?我正在使用annotate()。

+0

你是什麼意思的「物品」? – 2009-12-04 06:36:54

+0

請提供更多詳情。很難理解你想要做什麼 – luc 2009-12-04 06:37:36

+0

請顯示你的模型和結果應該是什麼樣子。 – 2009-12-04 07:34:59

回答

2
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) 
+0

謝謝...這是我需要的。 – Stephen 2009-12-04 12:41:13