I have a function that looks like this:
def post_count(self):
return self.thread_set.aggregate(num_posts=Count(\'post\'))[\'num_posts\']
<
You may want to look at writing a custom Manager object:
http://docs.djangoproject.com/en/1.1/topics/db/managers/
I haven't used aggregate()
, but that may let you write a custom manager to provide a filtered active_thread_set
and then do self.active_thread_set.aggregate(...)
. If not, it will let you do the custom SQL and add a num_posts
property onto the Thread
objects (see the PollManager.with_counts()
example.)