【问题标题】:django combine to query result and perform query on the combined query result on postgresdjango组合查询结果,在postgres上对组合查询结果进行查询
【发布时间】:2017-12-09 13:59:19
【问题描述】:

我正在尝试完成一个查询,以确定员工在一天内签到和签到的次数,该出勤对员工来说是唯一的,没有重复签到和签出,并选择最新的来区分。

本质是结合2个查询结果,对结果进行查询。

下面会更容易解释。

我有 2 个模型:

class Employee(models.Model):
    fullname = models.CharField(max_length=30, blank=False)    

class Attendance(models.Model):
    CHECKIN = 1
    CHECKOUT = 2
    ATTENDANCE_TYPE_CHOICES = (
        (CHECKIN, "Check In"),
        (CHECKOUT, "Check Out"),
    )
    employee = models.ForeignKey(Employee)
    activity_type = models.IntegerField(choices = ATTENDANCE_TYPE_CHOICES, default=CHECKIN)
    timestamp = models.DateTimeField(auto_now_add=True)

我有这 2 个查询来获取出勤员工的唯一 CheckIn 和 CheckOut。

unique_employee_attendance_checkin_today = Attendance.objects.filter(company=auth_employee.employee.company.id,
                                    activity_type=Attendance.CHECKIN,
                                    timestamp__date = today).order_by(
                                    'employee_id','-timestamp').distinct('employee_id')

unique_employee_attendance_checkout_today = Attendance.objects.filter(company=auth_employee.employee.company.id,
                                    activity_type=Attendance.CHECKOUT,
                                    timestamp__date = today).order_by(
                                    'employee_id','-timestamp').distinct('employee_id')

假设我有这些记录

Employee
{"id":1, "employee":"michael jackson",
"id":2, "fullname":"mariah carey",
"id":3, "fullname":"taylor swift"}

Attendance
{"id":1, "employee": 1,"activity_type": 1, timestamp: "2017-12-05 09:08", 
"id":2, "employee": 2,"activity_type": 1, timestamp: "2017-12-05 10:13",
"id":3, "employee": 3,"activity_type": 1, timestamp: "2017-12-05 11:30",
"id":4, "employee": 2,"activity_type": 2, timestamp: "2017-12-05 15:13", 
"id":5, "employee": 3,"activity_type": 2, timestamp: "2017-12-05 18:30", 
"id":6, "employee": 2,"activity_type": 1, timestamp: "2017-12-05 19:13", 
"id":7, "employee": 1,"activity_type": 2, timestamp: "2017-12-06 08:08"}

unique_employee_attendance_checkin_today - 将输出此结果

{"id":4, "employee": 2,"activity_type": 2, timestamp: "2017-12-05 15:13", 
"id":5, "employee": 3,"activity_type": 2, timestamp: "2017-12-05 18:30"}

unique_employee_attendance_checkout_today - 将输出此结果

{"id":1, "employee": 1,"activity_type": 1, timestamp: "2017-12-05 09:08", 
"id":6, "employee": 2,"activity_type": 1, timestamp: "2017-12-05 19:13"}
  1. 第 1 个问题在这里:

我想合并这两个查询

unique_employee_attendance_checkin_today + unique_employee_attendance_checkout_today

和

我想为员工创建一个 distinct,它会删除 2 条记录,所以输出结果是这样的。

{"id":1, "employee": 1,"activity_type": 1, timestamp: "2017-12-05 09:08", 
"id":5, "employee": 3,"activity_type": 2, timestamp: "2017-12-05 18:30"
"id":6, "employee": 2,"activity_type": 1, timestamp: "2017-12-05 19:13"}
  1. 第二个问题在这里:

然后根据 activity_type = 1 (CHECKIN) 和 activity_type = 2 (CHECKOUT) 的结果进行查询

上面的问题是,我如何将2个查询结果合并,并根据上述要求对合并后的查询进行查询。

【问题讨论】:

  • 您能否澄清一下为什么您在查询中使用created_at,而您的模型只有一个字段时间戳。
  • 对不起,我编辑了它,我把它改成了时间戳。
  • 所以,我还是一头雾水。您的签入示例 qs 有 activity_type 2(结帐)...如果我理解正确,您想要每个日期和员工中的最新内容,然后您想要过滤两个不同的 activity_types?
  • 我编辑了示例记录,希望它更清晰(参见 id=4&6 中的重复)。有 2 个查询,即 unique_employee_attendance_checkin_today + unique_employee_attendance_checkout_today。我想组合结果,并对该结果进行另一个查询 - 这与 employee_id 不同

标签: python django postgresql django-queryset


【解决方案1】:

组合查询(每位员工/日期的最新出勤率,无论类型如何)可以完全忽略 activity_type:

unique_employee_attendance_today = Attendance.objects.filter(
    company=auth_employee.employee.company.id,
    timestamp__date = today).order_by(
        'employee_id','-timestamp'
    ).distinct('employee_id')

现在您可以在嵌套查询中从该集合中过滤签到,例如:

Attendance.objects.filter(
    id__in=unique_employee_attendance_today,
    activity_type=Attendance.CHECKIN
)

这只会产生一个数据库查询。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 2021-07-02
    相关资源
    最近更新 更多