Designed django cart shopping

My shopping cart is designed in such a way that the user must be logged in.

How can I design the shopping cart with these two conditions that it must be stored in the database and not in the session and that the user does not need to login to add the product to the shopping cart?

my model :

    class Cart(models.Model):
        product = models.ForeignKey(Product, on_delete=models.CASCADE)
        user = models.ForeignKey(User, on_delete=models.CASCADE)    
        quantity = models.PositiveIntegerField()
    
        def __str__(self):
            return self.user.username

my view :

def cart_detail(request):
    cart = Cart.objects.filter(user_id=request.user.id)
    return render(request, 'cart/cart.html', {'cart': cart, })
Back to Top