如何在Django ORM中表示复杂的子查询和计算表?

时间:2019-04-24 15:25:29

标签: django django-orm

我有以下基本可用的SQL。我将如何在Django ORM中表示呢?我想避免运行完整的原始查询

我不确定如何在Django ORM中进行子查询以及如何正确执行笛卡尔积(由CROSS JOIN实现)

SELECT datum,
       alldata.worker_id,
       reporting_plan.project_id,
       SUM(effort::float)/60/60
FROM
  (SELECT DISTINCT datum,
                   reporting_plan.worker_id AS worker_id
   FROM
     (SELECT datum::date
      FROM generate_series('2019-05-01', '2019-12-31', '1 day'::interval) datum) AS dates
   CROSS JOIN reporting_plan
   ORDER BY datum,
            worker_id) AS alldata
LEFT OUTER JOIN reporting_plan ON alldata.worker_id = reporting_plan.worker_id
AND datum <= reporting_plan.end
AND datum >= reporting_plan.start
GROUP BY datum,
         alldata.worker_id,
         reporting_plan.worker_id,
         reporting_plan.project_id
ORDER BY datum,
         alldata.worker_id,
         reporting_plan.worker_id,
         reporting_plan.project_id

预期结果是一个列表,其中包含时间表中的所有日期以及所有工作人员以及匹配的计划信息(项目和工作量)。

谢谢!

编辑

根据@jimjimjim的反馈,我设法删除了CROSS JOIN,同时获得了相同的结果:

SELECT datum::date,
       alldata.worker_id,
       reporting_plan.project_id,
       SUM(effort::float)/60/60
FROM
  (SELECT DISTINCT generate_series ('2019-05-01', '2019-12-31', '1 day'::interval) AS datum,
                   worker_id
   FROM reporting_plan
   ORDER BY datum,
            worker_id) AS alldata
LEFT OUTER JOIN reporting_plan ON alldata.worker_id = reporting_plan.worker_id
AND datum <= reporting_plan.end
AND datum >= reporting_plan.start
GROUP BY datum,
         alldata.worker_id,
         reporting_plan.worker_id,
         reporting_plan.project_id
ORDER BY datum,
         alldata.worker_id,
         reporting_plan.project_id

2 个答案:

答案 0 :(得分:1)

您的问题与Reddit有点不同,通过这种方法,您将进行原始查询,但返回ORM对象。我将执行以下操作:

  1. 为查询创建模型:
    class WorkerEffort(models.Model):
        date = models.DateField(...)
        worker = models.ForeignKey(Worker, db_column="worker_id")
        project = models.ForeignKey(Project, db_column="project_id")
        effort = models.DecimalField()

        class Meta:
            managed = False

此处管理的信息:https://docs.djangoproject.com/en/2.2/ref/models/options/#managed

  1. 对该模型执行raw查询:
WorkerEffort.objects.raw('''
SELECT datum,
       alldata.worker_id,
       reporting_plan.project_id,
       SUM(effort::float)/60/60
FROM
  (SELECT DISTINCT datum,
                   worker_id
   FROM
     (select *, generate_series(start, end, '1 day'::interval) as datum from reporting_plan) 
     ) AS alldata
LEFT OUTER JOIN reporting_plan ON alldata.worker_id = reporting_plan.worker_id
GROUP BY datum,
         alldata.worker_id,
         reporting_plan.worker_id,
         reporting_plan.project_id
ORDER BY datum,
         alldata.worker_id,
         reporting_plan.worker_id,
         reporting_plan.project_id
''')

使用原始查询,您将能够通过WorkerEffort(查询模型)将ID与ORM对象进行匹配。

答案 1 :(得分:0)

这是否给您相同的结果?它消除了交叉联接,这将使使用ORM重写变得更容易,并且由于该系列是使用link = browser.table(class: 'alert-table').tbody.rows[1].cells.last.link(text: 'View') # <Watir::Anchor: located: false; {:class=>"alert-table", :tag_name=>"table"} --> {:tag_name=>"tbody"} --> {:index=>1} --> {:index=>-1} --> {:text=>"View", :tag_name=>"a"}> link.click reporting_plan.start生成的,因此您无需在联接条件中包括它们。如果需要对查询中给出的日期进行参数化,则可以将它们作为参数包含在ORM命令中,而不是查询本身。

end