Py学习  »  Django

django-是否可以在count()内执行queryset

Jack • 5 年前 • 1650 次点击  

我有下列型号

class Client(models.Model):
    ...

class Request(models.Model):
    ...
    client = models.ForeignKey(Client, on_delete=models.SET_NULL, null=True)
    completed = models.BooleanField()

我想得到一组客户端的所有已完成请求的计数。以下语句获取所有请求,但不检查请求是否已完成。

clients = Client.objects.filter(...).annotate(Count('request'))

我想要的是:

clients = Client.objects.filter(...).annotate(Count(request__completed=True))

我怎样才能做到这一点?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/39683
 
1650 次点击  
文章 [ 2 ]  |  最新文章 5 年前