我有下面列出的三个相关模型,我想运行一个聚合查询,以便为我提供按人员完成的任务总数,但只包含特定文件夹中的任务。
class Person(models.Model):
firstName = models.CharField(max_length=100, null=True, blank=True)
lastName = models.CharField(max_length=100, null=True, blank=True)
class Folder(models.Model):
title = models.CharField(max_length=254, null=True, blank=True)
assignees = models.ManyToManyField(Person, related_name="projects")
completedDate = models.DateTimeField(blank=True, null=True)
class Task(models.Model):
title = models.CharField(max_length=254, null=True, blank=True)
assignees = models.ManyToManyField(Person, related_name="tasks")
folders = models.ManyToManyField(Folder, related_name="tasks")
这是我的尝试查询:
tasks = Contact.objects.filter(tasks__folders__id='I58343DS89ASDF').distinct().filter(tasks__completedDate__gte='2016-10-01 00:00:00').annotate(total=Count('tasks'), name=F('firstName')).values('total', 'name')
这给了我一个错误的计数。所以我检查了这个Django查询生成的SQL语句。 SQL语句是:
SELECT DISTINCT COUNT(`wrike_task_assignees`.`task_id`) AS `total`, `wrike_contact`.`firstName` AS `name` FROM `wrike_contact` INNER JOIN `wrike_task_assignees` ON (`wrike_contact`.`id` = `wrike_task_assignees`.`contact_id`) INNER JOIN `wrike_task` ON (`wrike_task_assignees`.`task_id` = `wrike_task`.`id`) INNER JOIN `wrike_task_folders` ON (`wrike_task`.`id` = `wrike_task_folders`.`task_id`) INNER JOIN `wrike_task_assignees` T6 ON (`wrike_contact`.`id` = T6.`contact_id`) INNER JOIN `wrike_task` T7 ON (T6.`task_id` = T7.`id`) WHERE (`wrike_task_folders`.`folder_id` = I58343DS89ASDF AND T7.`completedDate` >= 2016-10-01 00:00:00) GROUP BY `wrike_contact`.`id` ORDER BY NULL
一个格式很好的版本是:
SELECT DISTINCT COUNT(ta.task_id) AS `total`, c.firstName AS `name`
FROM wrike_contact c INNER JOIN wrike_task_assignees ta ON (c.id = ta.contact_id)
INNER JOIN wrike_task t ON (ta.task_id = t.id)
INNER JOIN wrike_task_folders tf ON (t.id = tf.task_id)
INNER JOIN wrike_task_assignees T6 ON (c.id = T6.contact_id)
INNER JOIN wrike_task T7 ON (ta.task_id = T7.id)
WHERE (tf.folder_id = 'I58343DS89ASDF'
AND t.completedDate >= '2016-10-01 00:00:00')
GROUP BY c.id
ORDER BY NULL
请注意,一对INNER JOIN重复两次 - 不确定原因。
如果我删除那些重复的INNER JOIN然后运行查询,结果是正确的。这是我删除了重复的INNER JOIN段的SQL语句版本:
SELECT DISTINCT c.firstName AS name, COUNT(ta.task_id) AS `total`
FROM wrike_contact c
INNER JOIN wrike_task_assignees ta ON (c.id = ta.contact_id)
INNER JOIN wrike_task t ON (ta.task_id = t.id)
INNER JOIN wrike_task_folders tf ON (t.id = tf.task_id)
WHERE (tf.folder_id = 'I58343DS89ASDF'
AND t.completedDate >= '2016-10-01 00:00:00')
GROUP BY c.id
ORDER BY name;
有人可以解释为什么Django的查询会生成如此有问题的SQL语句以及如何修复它?
答案 0 :(得分:0)
以下查询对我有用:
fitlers = {'tasks__completedDate__gte': '2016-10-01 00:00:00', 'tasks__completedDate__lte': '2017-10-01 00:00:00'}
filtering = {'tasks__folders__id': settings.FOLDER_ID }
filtering.update(filters)
# Get number of general_tech_support_requests by person
gen_tasks = Contact.objects.filter(**filtering)\
.distinct()\
.annotate(total=Count('tasks'), assignee=F('firstName'))\
.values('total', 'assignee')