E
E
ErikHabr2022-02-22 14:13:26
Django
ErikHabr, 2022-02-22 14:13:26

How to create a cart for guest user on Django Rest Framework?

Guys, explain to me how to create a basket for a guest on the Django Rest Framework and so that after registration or authorization under his profile there will be a filled basket? I've seen examples of using sessions, but is this true since the examples were just related to Django and not DRF?


Here is an example of a view for adding goods to the cart, if user.is_authenticated user = user, and if not authorized, then who should make the user?:

class AddToCartAPIView(APIView):
    authentication_classes = [JWTAuthentication]

    def post(self, request, domain, product_slug):

        institution = Institution.objects.get(domain=domain)
        product = get_object_or_404(Product, slug=product_slug)
        user = self.request.user
        if user.is_authenticated and user.is_customer:
            user = user
        else:
            # make it that guest user can create cart
            # user = ?
            return Response(
                {"detail": "Authorize to add product to an order."})

        cart, cart_created = Cart.objects.get_or_create(
            institution=institution, customer=user)

        cart_item, cart_item_created = CartItem.objects.get_or_create(
            customer=user,
            product=product,
            cart=cart
        )

        if cart_created is False:
            if cart.items.filter(product__slug=product.slug).exists():
                cart_item.quantity += 1
                cart_item.save()
                return Response({"detail": "Product quantity updated"})
            else:
                cart.items.add(cart_item)
                return Response({"detail": "New product added"})
        else:
            cart.items.add(cart_item)
            return Response({"detail": "Cart created and product added"})

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2022-02-22
@bacon

There are many ways, each may have its own drawbacks, here you already need to look at how and what works for you. Options:
1. store such a basket not on the server, but on the client
2. create a separate table for such users, instead of the customer, the identification field through the session, jit from jwt, etc.
3. leave one table, make it possible for the customer to be null and add an identification field as in paragraph 2
4. leave it as it is now and automatically, create a user on the first addition
Well, think about how you will merge the baskets, there are also options, ideally for me, allow multiple carts.

R
Roman Kitaev, 2022-02-22
@deliro

Keep a separate signed cookie , regardless of the session and authorization will not affect it. But you need to understand that if the product pool of the logged in user and the anonymous user are different, then this will have to be handled manually.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question