Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 506543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:42:43+00:00 2026-05-13T06:42:43+00:00

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

  • 0

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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:42:44+00:00Added an answer on May 13, 2026 at 6:42 am

    OK, now that the question includes the model definitions, I submit to you that this should work, unless your version of Django doesn’t support some feature I use here (in which case, please let me know!):

    Post.objects.filter(thread__in=thread_set, status='active').aggregate(num_posts=Count('id'))
    

    Django allows __in filters to take a QuerySet to decide what the IN clause should look like in SQL, so if you pass thread__in=thread_set, Django will filter the posts so that only those whose thread field points to one of the ids of the threads in your thread_set remain for the aggregate call to see.

    This should filter the posts with just one db query with something like WHERE thread_id IN ... inside, rather than with one query per thread, which would indeed be horrid. If anything else happened, this would be a bug in Django…

    The result should be at most two queries to establish a Category‘s postcount — one to obtain thread_set and another one actually to count the posts. The alternative is to have a thread/post join to be filtered based on Thread‘s category field and Post‘s status field, which I wouldn’t necessarily expect to be that much faster. (I say ‘at most’, because I guess they could be fused automatically… Though I don’t think this would happen with current Django. Can’t check ATM, sorry.)

    EDIT: Django’s QuerySet API reference says this on __in filters:


    IN

    In a given list.

    Example:

    Entry.objects.filter(id__in=[1, 3, 4])
    

    SQL equivalent:

    SELECT ... WHERE id IN (1, 3, 4);
    

    You can also use a queryset to dynamically evaluate the list of values instead of providing a list of literal values:

    inner_qs = Blog.objects.filter(name__contains='Cheddar')
    entries = Entry.objects.filter(blog__in=inner_qs)
    

    This queryset will be evaluated as subselect statement:

    SELECT ... WHERE blog.id IN (SELECT id FROM ... WHERE NAME LIKE '%Cheddar%')
    

    The above code fragment could also be written as follows:

    inner_q = Blog.objects.filter(name__contains='Cheddar').values('pk').query
    entries = Entry.objects.filter(blog__in=inner_q)
    

    Changed in Django 1.1: In Django 1.0, only the latter piece of code is valid.

    This second form is a bit less readable and unnatural to write, since it accesses the internal query attribute and requires a ValuesQuerySet. If your code doesn’t require compatibility with Django 1.0, use the first form, passing in a queryset directly.


    So, I guess Django is capable of passing a single query to the db in the case at issue here. If the db’s query analyser does a good job, the effect might be very nearly optimal. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 296k
  • Answers 296k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Reading and writing to file shouldn't be difficult for you… May 13, 2026 at 7:02 pm
  • Editorial Team
    Editorial Team added an answer That's right, bytes come in sizes of 8 bits each1,… May 13, 2026 at 7:02 pm
  • Editorial Team
    Editorial Team added an answer When you call SelectNodes with "//BaaBaa" it returns all elements… May 13, 2026 at 7:02 pm

Related Questions

I am working on a testing framework for the software that my company writes.
I'm trying to create a tagcloud for an application. When I click a tag
Suppose I have a function that performs some task (this is in Python pseudocode):
Let's say I have a class that looks something like this: class Foo(Prop1:Int, Prop2:Int,
I have a class that I wish to expose as a remote service using

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.