2016-09-12 5 views
0
def most_common(self): 
    return self.get_queryset().annotate(
     num_times=models.Count(self.through.tag_relname()) 
     ).order_by('-num_times') 

これを編集して上位7個のタグのみを表示するにはどうすればよいですか?これはtaggitの関数であり、私は7人だけを見たいと思う。トップ7の最も一般的なタグTaggit

+2

(ozgur感謝)[正しいasnwerです:7 ] '? https://docs.djangoproject.com/en/1.10/topics/db/queries/#limiting-querysets – ozgur

+0

リストの理解。これはオーバーライドですmost_common()def most_common(self): return self.get_queryset()アノテーション( num_times = models.Count(self.through.tag_relname()) ).order_by( ' - num_times')] [: 7] –

+0

@ozgur。それからすべてを照会し、最初に7つの権利を得ます。 IsntはDB操作を減らすためのより良いオプションをオーバーライドしていないでしょうか? –

答えて

0

most_common()[:7]はどうですか?

YourModel.tags.most_common()[:7] 

docs.djangoproject.com/en/1.10/topics/db/queries/... -

ozgurこれは、どのようにmost_common `について

関連する問題