2016-08-11 105 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说明。

相关问题