Reputation: 77
I'm using PostgresSQL as DB, Django as backend and HTML, CSS, Javascript as Frontend. I have Multiple number of product Models
and I want to add all those models in one cart. So, I had Successfully add One of the model in cart. But
I want to add different products like Refrigerator, Fans, Washing Machine, ... etc. in one cart
So, how it will work?
The Codes goes here:
models.py
class Customer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
full_name = models.CharField(max_length=200)
joined_on = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.full_name
class Regrigeratior(models.Model):
image_src = models.URLField(max_length=300,null=True, blank=True)
name = models.CharField(max_length=200, db_index=True, verbose_name="processor name")
brand = models.CharField(max_length = 300, null=True, blank=True)
....
def __str__(self):
return self.name
class Fans(models.Model):
image_src = models.URLField(max_length=300,null=True, blank=True)
name = models.CharField(max_length=200, db_index=True, verbose_name="processor name")
brand = models.CharField(max_length = 300, null=True, blank=True)
....
def __str__(self):
return self.name
.
.
.
class Cart(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True, blank=True)
total = models.PositiveIntegerField(default=0)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return "Cart: " + str(self.id)
#
class CartProduct(models.Model):
cart = models.ForeignKey(Cart, on_delete=models.CASCADE)
refrigerator = models.ForeignKey(Refrigerator, on_delete=models.CASCADE)
rate = models.PositiveIntegerField()
quantity = models.PositiveIntegerField()
subtotal = models.PositiveIntegerField()
def __str__(self):
return "Cart: " + str(self.cart.id) + " CartProduct: " + str(self.id)
I have succesfully add Refrigerator Model in the cart
views.py
class AddToCartView(TemplateView):
template_name = "status.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
refrigerator_id = self.kwargs['ref_id']
refrigerator_obj = Refrigerator.objects.get(id = refrigerator_id)
cart_id = self.request.session.get("cart_id", None)
if cart_id:
cart_obj = Cart.objects.get(id = cart_id)
this_product_in_cart = cart_obj.cartproduct_set.filter(refrigerator = refrigerator_obj)
if this_product_in_cart.exists():
cartproduct = this_product_in_cart.last()
cartproduct.quantity += 1
cartproduct.subtotal += refrigerator_obj.price
cartproduct.save()
cart_obj.total += refrigerator_obj.price
cart_obj.save()
else:
cartproduct = CartProduct.objects.create(
cart = cart_obj, refrigerator = refrigerator_obj, rate = refrigerator_obj.price, quantity = 1, subtotal = refrigerator_obj.price)
cart_obj.total += processor_obj.price
cart_obj.save()
else:
cart_obj = Cart.objects.create(total=0)
self.request.session['cart_id'] = cart_obj.id
cartproduct = CartProduct.objects.create(
cart = cart_obj, refrigerator = refrigerator_obj, rate = refrigerator_obj.price, quantity = 1, subtotal = refrigerator_obj.price)
cart_obj.total += refrigerator_obj.price
cart_obj.save()
return context
So, Now I have Fan as a Model (and many more models). So how it will get add to the same cart?
Upvotes: 0
Views: 674
Reputation: 19
You should make a new model for products with fields image_src, name and brand. Making different models for different Categories is not effective.
Upvotes: 1
Reputation: 579
class Customer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
full_name = models.CharField(max_length=200)
joined_on = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.full_name
class ProductCategory(models.Model):
"""
storing product category to link with every product
a category like Fan, Refrigerator...etc
"""
name = models.CharField(max_length=200, db_index=True, verbose_name="product category")
def __str__(self):
return self.name
class Product(models.Model):
image_src = models.URLField(max_length=300,null=True, blank=True)
name = models.CharField(max_length=200, db_index=True, verbose_name="processor name")
category = model.ForeignKey(ProducyCategory,null=True,blank=True,on_delet=model.SET_NULL
brand = models.CharField(max_length = 300, null=True, blank=True)
....
def __str__(self):
return self.name
class CartItem(models.Model):
"""
Cart contain User and Product
"""
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True, blank=True)
product = models.ForeignKey(Product,null=True,blank=True,on_delet=model.SET_NULL)
total = models.PositiveIntegerField(default=0)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return "Cart: " + str(self.id)
Upvotes: 1