Django如何在注释max&#xff1f中获取其他字段的值
我想获取注释Max或Min中其他字段('status')的值,我该怎么做?
DB
用户 | 时间 | 状态 |
---|---|---|
name_a | 2022-01-03 11:23:40.000000 | 3 |
name_a | 2022-01-03 17: 56:41.000000 | 4 |
name_a | 2022-01-03 22:24:28.000000 | 1 |
name_a | 2022-01-04 08:24:28.000000 | 4 |
name_a | 2022-01-04 17:34:19.000000 | 4 |
name_b | 2022-01-03 08:29:18.000000 | 1 |
name_b | 2022-01-03 17:34:39.000000 | 4 |
# data_1 = Attendance.objects.filter(user__in=user_list)
data_2 = data_1.values('user', 'time__date').annotate(count=Count('time__date')).annotate(s_time=Min("time")).annotate(e_time=Max("time"))
#print
{'user': 'name_a', 'time__date': datetime.date(2022, 1, 3), 'count': 3, 's_time': datetime.datetime(2022, 1, 3, 11, 23, 40), 'e_time': datetime.datetime(2022, 1, 3, 22, 24, 28)}
我想得到:
{'user': 'name_a', 'time__date': datetime.date(2022, 1, 3), 'count': 3, 's_time': datetime.datetime(2022, 1, 3, 11, 23, 40), 'e_time': datetime.datetime(2022, 1, 3, 22, 24, 28), 's_status': '3', 'e_status': '1' }
我尝试添加过滤器但没有成功
data_2 = data_1.values('user', 'time__date').annotate(count=Count('time__date')).annotate(s_time=Min("time"), e_time=Max("time")).annotate(s_status=F("status"),filter=Q(time=Min("time"))).annotate(e_status=F("status"),filter=Q(time=Max("time")))
I want to get the value of other fields('status') in annotate Max or Min,how can I do it?
DB
user | time | status |
---|---|---|
name_a | 2022-01-03 11:23:40.000000 | 3 |
name_a | 2022-01-03 17:56:41.000000 | 4 |
name_a | 2022-01-03 22:24:28.000000 | 1 |
name_a | 2022-01-04 08:24:28.000000 | 4 |
name_a | 2022-01-04 17:34:19.000000 | 4 |
name_b | 2022-01-03 08:29:18.000000 | 1 |
name_b | 2022-01-03 17:34:39.000000 | 4 |
# data_1 = Attendance.objects.filter(user__in=user_list)
data_2 = data_1.values('user', 'time__date').annotate(count=Count('time__date')).annotate(s_time=Min("time")).annotate(e_time=Max("time"))
#print
{'user': 'name_a', 'time__date': datetime.date(2022, 1, 3), 'count': 3, 's_time': datetime.datetime(2022, 1, 3, 11, 23, 40), 'e_time': datetime.datetime(2022, 1, 3, 22, 24, 28)}
i want to get:
{'user': 'name_a', 'time__date': datetime.date(2022, 1, 3), 'count': 3, 's_time': datetime.datetime(2022, 1, 3, 11, 23, 40), 'e_time': datetime.datetime(2022, 1, 3, 22, 24, 28), 's_status': '3', 'e_status': '1' }
I tried adding filter but I didn't succeed
data_2 = data_1.values('user', 'time__date').annotate(count=Count('time__date')).annotate(s_time=Min("time"), e_time=Max("time")).annotate(s_status=F("status"),filter=Q(time=Min("time"))).annotate(e_status=F("status"),filter=Q(time=Max("time")))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
Attendance.objects.order_by('time').first().status
- 获取最短日期的状态。并使用Attendance.objects.order_by('time').last().status
- 获取最大日期的状态。You can use
Attendance.objects.order_by('time').first().status
- get status of min date. And useAttendance.objects.order_by('time').last().status
- get status of max date.