I have a function that looks like this:
def post_count(self):
return self.thread_set.aggregate(num_posts=Count('post'))['num_posts']
I only want to count posts that have their status marked as 'active'. Is there an easy way to add a filter before the Count function?
Model Definitions:
class Category(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField(max_length=100, blank=True, primary_key=True)
ordering = models.IntegerField(max_length=3, default=0)
@property
def thread_count(self):
return self.thread_set.all().count()
@property
def post_count(self):
return self.thread_set.aggregate(num_posts=Count('post'))['num_posts']
class Thread(models.Model):
user = models.ForeignKey(User)
category = models.ForeignKey(Category)
title = models.CharField(max_length=100)
slug = models.SlugField(max_length=100)
content = models.TextField()
created = models.DateTimeField(auto_now_add=True)
latest_activity = models.DateTimeField(auto_now_add=True)
class Post(models.Model):
thread = models.ForeignKey(Thread)
parent = models.ForeignKey('Post', null=True, blank=True)
display_name = models.CharField(max_length=100)
email = models.EmailField(db_index=True)
ip_address = models.IPAddressField(null=True, blank=True)
content = models.TextField()
status = models.CharField(choices=STATUS_CHOICES, max_length=25, db_index=True, default='approved')
created = models.DateTimeField()