Торговая корзина в django rest freamwork

I save the shopping cart in the Redis database. 

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

What should I do to create a shopping cart instead of the user ID in the code I wrote?



import redis

r = redis.Redis(host='localhost', port=6379, db=0)



class Cart:
    @classmethod
    def add_cart(cls, **kwargs):
        user_id = kwargs.get('user_id')
        product_id = kwargs.get('product_id')
        cart_name = f'{user_id}_{product_id}'

        if r.exists(cart_name):
            r.hincrby(cart_name, 'quantity', kwargs['quantity'])

        else:

            for field, value in kwargs.items():
                r.hset(cart_name, field, value)
        return cart_name

    @classmethod
    def get_cart(cls, user_id):
        cart = []
        for user_carts in r.scan_iter(f'{user_id}_*'):
            data = {key.decode('utf-8'): value.decode('utf-8') for key, value in r.hgetall(user_carts).items()}
            cart.append(data)
        return cart
Вернуться на верх