How to update the quantity of a custom order?

I am trying to update the quantity of a custom order in my bag. In a Django e-commerce application. I am getting an error stating: argument of type 'int' is not iterable, however I am wrapping the custom_order_id in a string. I need the bag to be in the format 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,
                    })

What I am trying to achieve is to be able to adjust the quantity of the custom order, if there is more than one custom order in the bag session variable.

Back to Top