Add more than one custom order to a bag session - Django e-commerce application

I am building a Django e-commerce application. I have implemented to ability to leave a custom order on the site and for users to be able to update the quantity, delete the custom order and checkout. I am now trying to refactor the code to allow more than one custom order to be placed in the users bag. What is currently happening is the second custom order is replacing the first custom order in the bag.

context.py file

 if 'custom_order' in bag:
        custom_order_id = bag['custom_order'] 
        custom_order = CustomOrder.objects.get(pk=custom_order_id)
        if 'quantity' in bag:
            total += bag['quantity'] * custom_order.price
            individual_total = bag['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': individual_total,
                'quantity': bag['quantity'],
                'product_name': custom_order.product_name,
                'price': custom_order.price,
         })
        else:
            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,
            })

my view where I am adding to the bag:

def add_custom_order_to_bag(request, custom_order_id):
    """Add custom order to the shopping bag """
    custom_order = CustomOrder.objects.get(pk=custom_order_id)
    bag = request.session.get('bag', {})
    
    bag['custom_order'] = custom_order_id
   
    request.session['bag'] = bag
    return redirect(reverse('view_bag'))

I tried adding a list into the view to handle the multiple custom orders, which seemed like the right way to go, but my context was complaining about receiving a list as the custom_order_id, I wasn't sure how to unpack it so as the context would process the multiple custom_order_id.

Back to Top