gpt4 book ai didi

Django Queryset - 通过 model.choices 获取计数

转载 作者:行者123 更新时间:2023-12-01 23:14:32 25 4
gpt4 key购买 nike

我有以下模型,其中包含有关客户 Leads 的所有详细信息。客户希望查看他的Leads 跟进状态。

class FollowUp(models.Model):
CALL_CHOICES = [("0", "Call Unanswered"),
("1", "Call Later"),
("2", "Visit Scheduled"),
("3", "Not Visited"),
("4", "Not reachable"),
("5", "Wrong Number"),
("6", "Not Interested"),
("7", "Deal Closed")]
status = models.CharField(_("Call Status"),
max_length = 20,
choices = CALL_CHOICES,
default = '1'
)
next_action_on = DateTimeField(_("Next Action"), auto_now_add=False,
null=True, blank=True)
reminder = models.BooleanField(_("reminder"), default=False)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)

class Lead(models.Model):
followups = models.ManyToManyField(FollowUp, verbose_name=_("Follow up"), blank=True)
...

如何获得每个status 选择的Lead 计数。比如

{
'Call Unanswered': 12 #Leads,
'Call Later': 10 # Leads
'Visit Scheduled': 20, #Leads,
...
}

最佳答案

试试这个:

Lead.objects.aggregate(
**{
choice[1]: Count(
'followups', filter=Q(followups__status=choice[0])
) for choice in Followup.CALL_CHOICES
}
)

这将返回如下内容:

{'Call Unanswered': 0,
'Call Later': 0,
'Visit Scheduled': 0,
'Not Visited': 0,
'Not reachable': 0,
'Wrong Number': 0,
'Not Interested': 0,
'Deal Closed': 0}

关于Django Queryset - 通过 model.choices 获取计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69263129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com