Filtering the Aggregate in the Django ORM

后端 未结 5 936
误落风尘
误落风尘 2021-02-08 14:49

I have a function that looks like this:

def post_count(self):
        return self.thread_set.aggregate(num_posts=Count(\'post\'))[\'num_posts\']
<
5条回答
  •  轮回少年
    2021-02-08 15:28

    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.)

提交回复
热议问题