Как обновить количество в пользовательском заказе?

Я пытаюсь обновить количество пользовательского заказа в моей сумке. В приложении электронной коммерции Django. Я получаю ошибку: argument of type 'int' is not iterable, однако я оборачиваю custom_order_id в строку. Мне нужно, чтобы в сумке был формат bag = {[{195,2},{187:3}]}

def custom_order_update(request, custom_order_id):
    """ Updates the quantity of custom orders added to the bag"""
    bag = request.session.get('bag', {})
    quantity = int(request.POST.get('quantity'))
    custom_orders = bag.get('custom_orders', [])

    for quantity_dict in custom_orders:
        if str(custom_order_id) in quantity_dict:
            quantity_dict[str(custom_order_id)] = quantity
            break

    bag['custom_orders'] = custom_orders

    request.session['bag'] = bag

    return redirect(reverse('view_bag'))

Traceback:

Traceback (most recent call last):
  File "/workspace/.pip-modules/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/workspace/.pip-modules/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/workspace/jenas-creations/custom_order/views.py", line 90, in custom_order_update
    if str(custom_order_id) in quantity_dict:

Exception Type: TypeError at /custom_order/update_custom_order/197/
Exception Value: argument of type 'int' is not iterable

context.py

   if 'custom_orders' in bag:
        if isinstance(bag.get('custom_orders'), list):
            if any(isinstance(item, dict) for item in bag.get('custom_orders')):
                for quantity_dict in bag['custom_orders']:
                    if isinstance(quantity_dict, dict):
                        for custom_order_id, quantity in quantity_dict.items():
                            custom_order = CustomOrder.objects.get(pk=custom_order_id)
                            total += quantity * custom_order.price
                            bag_items.append({
                            'product_id': custom_order.product_id,
                            'custom_order_id': custom_order_id,
                            'category': custom_order.category,
                            'material': custom_order.material,
                            'individual_total': quantity * custom_order.price,
                            'quantity': quantity,
                            'product_name': custom_order.product_name,
                            'price': custom_order.price,
                        })
                    else:
                        custom_order = CustomOrder.objects.get(pk=quantity_dict)
                        total += custom_order.price
                        bag_items.append({
                            'product_id': custom_order.product_id,
                            'custom_order_id': quantity_dict,
                            'category': custom_order.category,
                            'material': custom_order.material,
                            'individual_total': custom_order.price,
                            'quantity': 1,
                            'product_name': custom_order.product_name,
                            'price': custom_order.price,
                    })


            else:
                custom_order_ids = bag['custom_orders'] 
                for custom_order_id in custom_order_ids:
                    custom_order = CustomOrder.objects.get(pk=custom_order_id)
                    total += custom_order.price
                    bag_items.append({
                        'product_id': custom_order.product_id,
                        'custom_order_id': custom_order_id,
                        'category': custom_order.category,
                        'material': custom_order.material,
                        'individual_total': custom_order.price,
                        'quantity': 1,
                        'product_name': custom_order.product_name,
                        'price': custom_order.price,
                    })

Я пытаюсь добиться того, чтобы иметь возможность регулировать количество пользовательского заказа, если в переменной сеанса сумки находится более одного пользовательского заказа.

Вернуться на верх