if request.method == 'POST':
body = request.body.decode("utf-8")
orders = body.split('\n')
big_bags_list = []
'''
Создаем список из привязанных big_bags к посылкам
'''
for parcel in orders:
db = connect(table='parcels',
field='document_id',
equally=parcel,
item='big_bag_id')
big_bags_list.append(db)
order_with_big_bags = dict.fromkeys(set(big_bags_list), [])
order_with_big_bags[db].append(parcel)
return Response(order_with_big_bags)
{4320: ['28-1661-0753'], 4321: ['28-1661-0753'], 4308: ['28-1661-0753'], 4309: ['28-1661-0753'], 4310: ['28-1661-0753'], 4311: ['28-1661-0753'], 4312: ['28-1661-0753'], 4313: ['28-1661-0753'], 4314: ['28-1661-0753'], 4315: ['28-1661-0753'], 4316: ['28-1661-0753'], 4317: ['28-1661-0753'], 4318: ['28-1661-0753'], 4319: ['28-1661-0753']}
order_with_big_bags
, который объявлен в теле цикла, естественно вам вернет последнее значение. Объявите перед циклом if request.method == 'POST':
body = request.body.decode("utf-8")
orders = body.split('\n')
big_bags_list = []
order_with_big_bags = {}
'''
Создаем список из привязанных big_bags к посылкам
'''
for parcel in orders:
bb_id = connect(table='parcels',
field='document_id',
equally=parcel,
item='big_bag_id')
big_bags_list.append(bb_id)
order_with_big_bags = dict.fromkeys(set(big_bags_list), [])
order_with_big_bags[bb_id].append(parcel)
return Response(order_with_big_bags)
{
"4320": [
"28-1661-0896"
],
"4321": [
"28-1661-0907"
],
"4308": [
"28-1661-0753"
],
"4309": [
"28-1661-0764"
],
"4310": [
"28-1661-0775"
],
"4311": [
"28-1661-0786"
],
"4312": [
"28-1661-0797"
],
"4313": [
"28-1661-0808"
],
"4314": [
"28-1661-0830"
],
"4315": [
"28-1661-0841"
],
"4316": [
"28-1661-0852"
],
"4317": [
"28-1661-0863"
],
"4318": [
"28-1661-0874"
],
"4319": [
"28-1661-0885"
]
}
if request.method == 'POST':
body = request.body.decode("utf-8")
orders = body.split('\n')
big_bags_list = []
'''
Создаем словарь из привязанных big_bags к посылкам
'''
for parcel in orders:
big_bag = connect(table='parcels',
field='document_id',
equally=parcel,
item='big_bag_id')
big_bags_list.append(big_bag)
order_with_big_bags = dict.fromkeys(set(big_bags_list), [])
for parcel in orders:
big_bag = connect(table='parcels',
field='document_id',
equally=parcel,
item='big_bag_id')
order_with_big_bags[big_bag].append(parcel)
return Response(order_with_big_bags)
{
"4320": [
"28-1661-0907",
"28-1661-0896",
"28-1661-0885",
"28-1661-0874",
"28-1661-0863",
"28-1661-0852",
"28-1661-0841",
"28-1661-0830",
"28-1661-0808",
"28-1661-0797",
"28-1661-0786",
"28-1661-0775",
"28-1661-0764",
"28-1661-0753"
],
"4321": [
"28-1661-0907",
"28-1661-0896",
"28-1661-0885",
"28-1661-0874",
"28-1661-0863",
"28-1661-0852",
"28-1661-0841",
"28-1661-0830",
"28-1661-0808",
"28-1661-0797",
"28-1661-0786",
"28-1661-0775",
"28-1661-0764",
"28-1661-0753"
], ....
enumerate
и i
добавлены т.к. я прохожусь по листам.orders = ['28-1661-0907', '28-1661-0896', '28-1661-0885', '28-1661-0874', '28-1661-0863', '28-1661-0852',
'28-1661-0841', '28-1661-0830', '28-1661-0808', '28-1661-0797', '28-1661-0786', '28-1661-0775',
'28-1661-0764', '28-1661-0753']
bb_ids = [4321, 4320, 4319, 4318, 4317, 4316, 4315, 4314, 4313, 4312, 4311, 4310, 4309, 4308]
order_with_big_bags = {}
for i, parcel in enumerate(orders):
bb_id = bb_ids[i]
try:
if isinstance(order_with_big_bags[bb_id], list):
order_with_big_bags[bb_id].append(parcel)
except KeyError:
order_with_big_bags[bb_id] = [parcel]
print(order_with_big_bags)
def get_bb_id(parcel):
return connect(table='parcels',
field='document_id',
equally=parcel,
item='big_bag_id')
order_with_big_bags = {get_bb_id(parcel): [parcel] for parcel in orders}
print(order_with_big_bags)
if request.method == 'POST':
body = request.body.decode("utf-8")
orders = body.split('\n')
order_with_big_bags = {}
for parcel in orders:
bb_id = connect(table='parcels',
field='document_id',
equally=parcel,
item='big_bag_id')
try:
if isinstance(order_with_big_bags[bb_id], list):
order_with_big_bags[bb_id].append(parcel)
except KeyError:
order_with_big_bags[bb_id] = [parcel]
return Response(order_with_big_bags)
if request.method == 'POST':
body = request.body.decode("utf-8")
orders = body.split('\n')
order_with_big_bags = {}
for parcel in orders:
bb_id = connect(table='parcels',
field='document_id',
equally=parcel,
item='big_bag_id')
if bb_id not in order_with_big_bags:
order_with_big_bags[bb_id] = [parcel]
else:
order_with_big_bags[bb_id].append(parcel)
return Response(order_with_big_bags)