Prakash Kumar
Prakash Kumar

Reputation: 2594

How use filter in following case .

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

Answers (1)

wim
wim

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

Related Questions