Filtering the Aggregate in the Django ORM

后端 未结 5 851
误落风尘
误落风尘 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:31

    I have been looking into something similar and have not found a great solution. I'm using something like this:

    def post_count(self):
            return len(Post.objects.filter(someModel = self).filter(active_status = 1))
    

    It's not great, but I don't think that Django allows you to filter based on the secondary model aggregations and annotations. I'll be checking to see if anyone comes up with a better solution.

提交回复
热议问题