Django prevents many-to-many communication from being established before the model instance is saved

I need to have a shopping cart and wishlist for unauthorized users on multiple pages, so I decided to add middleware and create a shopping cart for it based on the data in request.session, but django doesn’t allow a many-to-many connection before saving the model instance. How to proceed? Set just a list with products instead of an instance (it’s difficult because my context processor uses the models api)?

def shop(get_response):
    
    def middleware(request):

        If not request.user.is_authenticated:
            
            basket_products = request.session.get('basket', [])
            wishlist_products = request.session.get('wishlist', [])
            basket = Basket()
            wishlist = WishList()
            
            for product in basket_products:
                basket.products.add(Product.objects.get(id=product))
                
            for product in wishlist_products:
                wishlist.products.add(Product.objects.get(id=product))
            
            request.user.basket = basket
            request.user.wishlist = wishlist
            
        else:
            ...
            #user already have basket and wishlist
            
        
        
        
        response = get_response(request)



        return response
    
    return middleware

That’s one option.

Another option would be to create models for your shopping cart, where each cart is associated with a session id. (Perhaps in addition to being associated with a user if you have authenticated users as well.)

You’d need to manage those tables to delete rows with expired sessions, but if you’re using the database backend for sessions you’ll be doing something like that already.

Would it be too inefficient for each visitor to create a row in the table?

Not at any reasonable degree of scale that you’re going to encounter, no.

(But then if you did reach that level of activity, there are many architectural changes you’d need to make - this one being one of the lesser issues.)

I thought that possible architectural decisions to optimize the business logic are made long before there is a lot of activity on the service, but that’s not the case, am I understanding you correctly? If so, then doesn’t it then become too costly to modify for performance when the service already has many users?

This isn’t a “business logic” issue, this is a deployment issue when you’re talking about “webscale” issues.

However, on “Day 1”, you are not competing with Amazon, Wikipedia, Instagram, Facebook, or TikTok, and it’s, quite frankly, silly and wasteful to architect for that scale when you don’t have any traffic yet. By my very rough, off-the-cuff estimates, fewer than 0.000001% of all websites in the world need to take these sorts of scale into consideration, and you will never convince me that you aren’t in that 99.999999% until you demonstrate that you are.

And if you do get to that point, you will have the time (and financial resources) to be able to accommodate it. All those sites listed above started small, and grew. None of them were designed to handle the level of traffic they currently do - and they all successfully grew into it.

2 Likes