I want to summarize all attendance records of each person every day,in my current solution, i need to get all records and then use for
loop, is there a better way to get the queryset like the following:
class AttendanceRecord(models.Model):
user_id = models.IntegerField()
device_id = models.IntegerField()
f_time = models.DateTimeField(auto_created=True)
# How to group record by user and get a queryset like the following?
[
[
{'user_id': 1, 'device_id': 1, 'f_time': '2020-11-11 8:00:00'},
{'user_id': 1, 'device_id': 1, 'f_time': '2020-11-11 18:00:00'}
],
[
{'user_id': 2, 'device_id': 2, 'f_time': '2020-11-11 8:00:00'},
{'user_id': 2, 'device_id': 2, 'f_time': '2020-11-11 18:00:00'}
],
]
# or like this
[
{user_id1: [
{'device_id': 1, 'f_time': '2020-11-11 8:00:00'},
{'device_id': 1, 'f_time': '2020-11-11 18:00:00'}
]},
{user_id2: [
{'device_id': 2, 'f_time': '2020-11-11 8:00:00'},
{'device_id': 2, 'f_time': '2020-11-11 18:00:00'}
]},
]