前些日子在开发业务的一个模块时,有一个需求:需要根据key去重后group by 部门名称进行总数的计算,首次代码是这么编写的,因为只有当天的数据,所以接口验证没什么问题:
objs = TestPlanOverdueDetail.objects.filter(
create_time__lte=datetime.datetime.strptime(get_work_month_date(month)[1], "%Y-%m-%d"),
create_time__gte=datetime.datetime.strptime(get_work_month_date(month)[0], "%Y-%m-%d"),
).values("key").distinct().values_list("reporter__dept_name").annotate(sum=Count("reporter"))
第二天早上拿到第二天的数据之后,再次验证了数据,发现去重无效,重复数据也被分组统计到了,我将代码进行了分部打