Home > database >  Add product to cart using python in Django
Add product to cart using python in Django

Time:10-24

I want to make a simple function that collects the product ID and adds it to the user's shopping cart by clicking the "add to cart" button. I have the shopping cart table and product table created but am struggling to find the correct function to add the product to the cart. Anything helps I am very new to Python and Django and am looking for some help to get me on the right track.

class Cart(models.Model):
cart_id = models.Charfield(primary_key=True)
total = models.DecimalField(max_digits=9,decimal_places=2)  
quantity = models.IntegerField()  
products = models.ManyToManyField(Product)

class Product(models.Model):
prod_id = models.CharField(max_length=15, null=True)
name = models.CharField(max_length=200)
price = models.DecimalField(max_digits=10,decimal_places=2)  
description = models.TextField(max_length=1000)

def cart_add(request, prod_id):
    item = Product.objects.get(pk=prod_id)

I know this isn't much to work with but anything helps, I am just looking for some help with creating the add to cart function

CodePudding user response:

You need a relation between Cart* and User

class Cart(models.Model):
    cart_id = models.Charfield(primary_key=True)
    total = models.DecimalField(max_digits=9,decimal_places=2)  
    quantity = models.IntegerField()  
    products = models.ManyToManyField(Product)
    user = models.OneToOneField(User)

class Product(models.Model):
    prod_id = models.CharField(max_length=15, null=True)
    name = models.CharField(max_length=200)
    price = models.DecimalField(max_digits=10,decimal_places=2)  
    description = models.TextField(max_length=1000)

def cart_add(request, prod_id):
    item = Product.objects.get(pk=prod_id)
    request.user.cart.add(item)

CodePudding user response:

You need to have a relation between Cart model and User model, So that you can save product to respective user's cart.

Moreover a good approach would be adding another model named CartItem and keeping the product information in that model instead of storing product directly to Cart model. This is because when we add a product to cart, we might need to save and later update extra information about the product for example 'quantity'.

class Cart(models.Model):
    cart_id = models.Charfield(primary_key=True)
    total = models.DecimalField(max_digits=9,decimal_places=2)  
    quantity = models.IntegerField()  
    user = models.OneToOneField(User)

class CartItem(models.Model):
    cart = models.ForeignKey(Cart, on_delete=models.CASCADE)
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    product_quantity = models.IntegerField(default=0)
    user = models.OneToOneField(User)

So in this case, all you need to do is create a new CartItem object in add to cart view.:

def cart_add(request, prod_id, qty):
    item = Product.objects.get(pk=prod_id) 
    cart_obj = Cart.objects.get(user=request.user)
    Cart.objects.create(cart=cart_obj, product=item, product_quantity=qty)

Hope this helps :)

  • Related