我正试着进入djangos annotate,但不太清楚它到底是如何工作的。你知道吗
我有一个函数,我想在其中注释一个客户查询集,过滤它们并返回客户数
def my_func(self):
received_signatures = self.customer_set.annotate(Count('registrations').filter().count()
现在对于过滤部分,我有一个问题要解决。我想筛选的对象是我的received_signatures
,这是在我的py客户你知道吗
def received_signatures(self):
signatures = [reg.brought_signature for reg in self.registrations.all() if reg.status == '1_YES']
if len(signatures):
return all(signatures)
else:
return None
brough_signature
是一个DB字段
那么,我如何对查询集进行注释,过滤收到的\u签名,然后返回一个数字呢?你知道吗
相关型号信息:
class Customer(models.Model):
brought_signature = models.BooleanField(u'Brought Signature', default=False)
class Registration(models.Model):
brought_signature = models.BooleanField(u'Brought Signature', default=False)
status = models.CharField(u'Status', max_length=10, choices=STATUS_CHOICES, default='4_RECEIVED')
注意:参与者和注册可以有brought_signature
。我在我的程序中有一个设置,允许我要么a)在我的参与者身上标记只带了签名(这意味着他所有的注册都带了签名),要么B)在他所有的注册上标记带了签名
在这种情况下,选项B)是相关的。通过我的received_signatures
,我检查客户是否为每个状态为“1是”的注册带来了每个签名,我想统计所有这样做的客户并返回一个数字(然后在pygal图表的另一个函数中使用)
如果我理解正确,您需要检查给定的
Customer
和status == '1_YES
的Registration
的所有^{是否应该具有属性.brought_signature = True
,并且至少应该有这样的值。这有几种方法。你知道吗我们可以这样写:
因此,我们在这里首先对状态为
1_YES
的registration
计算一个值minb
,即这些Registration
中brought_signature
的最小值。因此,如果相关Registration
的brought_signature
是False
(在通常为0
的数据库中),那么Min(..)
也是False
。如果所有的brought_signature
都是True
(在一个通常是1
的数据库中),那么结果就是1
,我们就可以根据minb
应该大于0
的事实进行过滤。你知道吗因此,具有无
Registration
的Customer
将不被计数,Customer
具有状态1_YES
的无Registration
的Customer
将不被计数,Customer
具有状态1_YES
的Registration
的Customer
将不被计数。只有Customer
个^{Registration
个1_YES
状态的^{Registration
个brough_signature = True
都被计算在内。你知道吗相关问题 更多 >
编程相关推荐