2016-08-11 85 views
1

我會怎麼做以下查詢:如何在以下情況下使用過濾器。

Post.objects.filter(長度( '意見')> 5)

class Post(models.Model): 
    title = models.CharField(max_length=200) 
    text = models.TextField() 
class Comment(models.Model): 
    text = models.CharField(max_length=100) 
    post = models.ForeignKey(Post) 

基本上,我需要得到所有posts項目有不止5評論。

回答

2

像這樣:

from django.db.models import Count 
Post.objects.annotate(num_comments=Count('comment')).filter(num_comments__gt=5) 

的方法在aggregation section of the docs說明。

相關問題