Как сгруппировать результаты в массив на основе нескольких одинаковых значений, Django Model
У меня есть массив следующим образом
[
{
"WarehouseId": 1,
"ShippingCarrierId": 1,
"PostalCodeType": "ShipToCustomer",
"TimeStart": "1970-01-01T06:00:00.000Z",
"TimeEnd": "1970-01-01T15:59:00.000Z",
"PickupTimeSlot": "PM",
"DaysToAdd": 0,
"PickupTime": "1970-01-01T17:00:00.000Z"
},
{
"WarehouseId": 1,
"ShippingCarrierId": 1,
"PostalCodeType": "ShipToCustomer",
"TimeStart": "1970-01-01T16:00:00.000Z",
"TimeEnd": "1970-01-01T23:59:00.000Z",
"PickupTimeSlot": "AM",
"DaysToAdd": 1,
"PickupTime": "1970-01-01T11:00:00.000Z"
},
{
"WarehouseId": 1,
"ShippingCarrierId": 1,
"PostalCodeType": "ShipToCustomer",
"TimeStart": "1970-01-01T00:00:00.000Z",
"TimeEnd": "1970-01-01T05:59:00.000Z",
"PickupTimeSlot": "AM",
"DaysToAdd": 0,
"PickupTime": "1970-01-01T11:00:00.000Z"
},
{
"WarehouseId": 2,
"ShippingCarrierId": 2,
"PostalCodeType": "ShipToCustomer",
"TimeStart": "1970-01-01T00:00:00.000Z",
"TimeEnd": "1970-01-01T15:59:00.000Z",
"PickupTimeSlot": "PM",
"DaysToAdd": 0,
"PickupTime": "1970-01-01T17:00:00.000Z"
},
{
"WarehouseId": 2,
"ShippingCarrierId": 2,
"PostalCodeType": "ShipToCustomer",
"TimeStart": "1970-01-01T16:00:00.000Z",
"TimeEnd": "1970-01-01T23:59:00.000Z",
"PickupTimeSlot": "AM",
"DaysToAdd": 1,
"PickupTime": "1970-01-01T11:00:00.000Z"
},
{
"WarehouseId": 1,
"ShippingCarrierId": 3,
"PostalCodeType": "ShipToDS",
"TimeStart": "1970-01-01T00:00:00.000Z",
"TimeEnd": "1970-01-01T15:59:00.000Z",
"PickupTimeSlot": "PM",
"DaysToAdd": 0,
"PickupTime": "1970-01-01T17:00:00.000Z"
},
{
"WarehouseId": 1,
"ShippingCarrierId": 3,
"PostalCodeType": "ShipToDS",
"TimeStart": "1970-01-01T16:00:00.000Z",
"TimeEnd": "1970-01-01T23:59:00.000Z",
"PickupTimeSlot": "AM",
"DaysToAdd": 1,
"PickupTime": "1970-01-01T11:00:00.000Z"
},
{
"WarehouseId": 2,
"ShippingCarrierId": 4,
"PostalCodeType": "ShipToDS",
"TimeStart": "1970-01-01T00:00:00.000Z",
"TimeEnd": "1970-01-01T15:59:00.000Z",
"PickupTimeSlot": "PM",
"DaysToAdd": 0,
"PickupTime": "1970-01-01T17:00:00.000Z"
},
{
"WarehouseId": 2,
"ShippingCarrierId": 4,
"PostalCodeType": "ShipToDS",
"TimeStart": "1970-01-01T16:00:00.000Z",
"TimeEnd": "1970-01-01T23:59:00.000Z",
"PickupTimeSlot": "AM",
"DaysToAdd": 1,
"PickupTime": "1970-01-01T11:00:00.000Z"
},
{
"WarehouseId": 1,
"ShippingCarrierId": 5,
"PostalCodeType": "ShipToDS",
"TimeStart": "1970-01-01T00:00:00.000Z",
"TimeEnd": "1970-01-01T16:22:00.000Z",
"PickupTimeSlot": "PM",
"DaysToAdd": 0,
"PickupTime": "1970-01-01T17:00:00.000Z"
},
{
"WarehouseId": 1,
"ShippingCarrierId": 5,
"PostalCodeType": "ShipToDS",
"TimeStart": "1970-01-01T16:23:00.000Z",
"TimeEnd": "1970-01-01T23:59:00.000Z",
"PickupTimeSlot": "AM",
"DaysToAdd": 1,
"PickupTime": "1970-01-01T11:00:00.000Z"
},
{
"WarehouseId": 2,
"ShippingCarrierId": 6,
"PostalCodeType": "ShipToDS",
"TimeStart": "1970-01-01T00:00:00.000Z",
"TimeEnd": "1970-01-01T15:59:00.000Z",
"PickupTimeSlot": "PM",
"DaysToAdd": 0,
"PickupTime": "1970-01-01T17:00:00.000Z"
},
{
"WarehouseId": 2,
"ShippingCarrierId": 6,
"PostalCodeType": "ShipToDS",
"TimeStart": "1970-01-01T16:00:00.000Z",
"TimeEnd": "1970-01-01T23:59:00.000Z",
"PickupTimeSlot": "AM",
"DaysToAdd": 1,
"PickupTime": "1970-01-01T11:00:00.000Z"
},
{
"WarehouseId": 1,
"ShippingCarrierId": 1,
"PostalCodeType": "ShipToDS",
"TimeStart": "1970-01-01T06:00:00.000Z",
"TimeEnd": "1970-01-01T15:59:00.000Z",
"PickupTimeSlot": "PM",
"DaysToAdd": 0,
"PickupTime": "1970-01-01T17:00:00.000Z"
},
{
"WarehouseId": 1,
"ShippingCarrierId": 1,
"PostalCodeType": "ShipToDS",
"TimeStart": "1970-01-01T16:00:00.000Z",
"TimeEnd": "1970-01-01T23:59:00.000Z",
"PickupTimeSlot": "AM",
"DaysToAdd": 1,
"PickupTime": "1970-01-01T11:00:00.000Z"
},
{
"WarehouseId": 1,
"ShippingCarrierId": 1,
"PostalCodeType": "ShipToDS",
"TimeStart": "1970-01-01T00:00:00.000Z",
"TimeEnd": "1970-01-01T05:59:00.000Z",
"PickupTimeSlot": "AM",
"DaysToAdd": 0,
"PickupTime": "1970-01-01T11:00:00.000Z"
},
{
"WarehouseId": 2,
"ShippingCarrierId": 2,
"PostalCodeType": "ShipToDS",
"TimeStart": "1970-01-01T00:00:00.000Z",
"TimeEnd": "1970-01-01T15:59:00.000Z",
"PickupTimeSlot": "PM",
"DaysToAdd": 0,
"PickupTime": "1970-01-01T17:00:00.000Z"
},
{
"WarehouseId": 2,
"ShippingCarrierId": 2,
"PostalCodeType": "ShipToDS",
"TimeStart": "1970-01-01T16:00:00.000Z",
"TimeEnd": "1970-01-01T23:59:00.000Z",
"PickupTimeSlot": "AM",
"DaysToAdd": 1,
"PickupTime": "1970-01-01T11:00:00.000Z"
}
]
Я хочу сгруппировать их по WarehouseId, ShippingCarrierId и PostalCodeType. Каждый объект будет иметь массив объектов для Timestart, TimeEnd и т.д.
Как я могу этого добиться
PickupTimeTable: [{
WarehouseId: 1,
ShippingCarrierId: 1,
PostalCodeType: 'ShipToCustomer',
PickupTimeSlots: [{
StartTime: '06:00:00.0000000',
EndTime: '15:59:00.0000000',
Slot: 'PM',
PickupTime: '17:00:00.0000000',
DaysToAdd: 0
},
{
StartTime: '16:00:00.0000000',
EndTime: '23:59:00.0000000',
Slot: 'AM',
PickupTime: '11:00:00.0000000',
DaysToAdd: 1
},
{
StartTime: '00:00:00.0000000',
EndTime: '05:59:00.0000000',
Slot: 'AM',
PickupTime: '11:00:00.0000000',
DaysToAdd: 0
},
]
}, ]
Вам нужна простая агрегация данных.
data = [{...}] # This is the first data you get from database.
result = dict()
for item in data:
group_by_str = f"{item['WarehouseId']}-{item['ShippingCarrierId']}-{item['PostalCodeType']}"
if group_by_str in result:
result[group_by_str]['PickupTimeSlots'].append({"StartTime": item['StartTime'], ...})
else:
result[group_by_str] = {"WarehouseId": item['WarehouseId'], "ShippingCarrierId": item['ShippingCarrierId'], "PostalCodeType": item['PostalCodeType'], 'PickupTimeSlots': [{"StartTime": item['StartTime'], ...}]}
result = list(result.values())
Я не полностью указал поля, но я думаю, что вы можете продолжить с этого.