我正在尝试查找一个查询,以获取特定日期的最新出勤率列表,其中包含唯一已签入的员工(CHECKIN=1)
下面是我的模型,记录和我想要完成的。你知道吗
我的模型有两种:
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)
假设我有以下记录:
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", -interested in this for the activity type (1 for CHECKIN) and the date 2017-12-05, last of that employee id for that day
"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", -interested in this for the activity type (1 for CHECKIN) and the date 2017-12-05, last of that employee id for that day
"id":7, "employee": 3,"activity_type": 1, timestamp: "2017-12-05 20:30", -interested in this for the activity type (1 for CHECKIN) and the date 2017-12-05, last of that employee id for that day
"id":8, "employee": 1,"activity_type": 2, timestamp: "2017-12-06 08:08"}
我很感兴趣,因此它仅根据活动类型1输出这2条记录,以便签入和日期为2017-12-05,并且员工id必须是唯一和最新的:
{"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"
"id":7, "employee": 3,"activity_type": 1, timestamp: "2017-12-05 20:30"}
我用的是Postgres9.4
如何使用Django queryset生成预期结果?你知道吗
按
activity_type
筛选考勤,按employee_id
和-timestamp
排序,然后在employee_id
上使用distinct相关问题 更多 >
编程相关推荐