Reputation: 2594
How would I do the following query:
Post.objects.filter(Length('comment')>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)
Basically, I need to get all posts
items having more than 5 comment.
Upvotes: 1
Views: 26
Reputation: 362517
Something like this:
from django.db.models import Count
Post.objects.annotate(num_comments=Count('comment')).filter(num_comments__gt=5)
The method is described in the aggregation section of the docs.
Upvotes: 2