Как вычитать значения при добавлении товара в shopcart
Я успешно вычитаю количество товара при оформлении заказа. Я также успешно вычитаю значения для пользователя, когда товар добавляется в shopcart. Проблема в том, что я не могу вычесть вариант товара. Появляется сообщение "Вариантов, соответствующих запросу, не существует"
.Здесь приведен мой код.
models.py
class Product(models.Model):
VARIANTS = (
('None', 'None'),
('Size', 'Size'),
('Color', 'Color'),
)
user = models.ForeignKey(User, on_delete=models.CASCADE)
category = models.ForeignKey(Category, on_delete=models.CASCADE) #many to one relation with Category
title = models.CharField(max_length=150)
image = models.ImageField(null=False, upload_to='images/')
price = models.DecimalField(max_digits=12, decimal_places=2,default=0)
amount = models.IntegerField(default=0)
variant = models.CharField(max_length=10, choices=VARIANTS, default='None')
detail = RichTextUploadingField()
slug = models.SlugField(null=False, unique=True)
def __str__(self):
return self.title
class Variants(models.Model):
title = models.CharField(max_length=100, blank=True,null=True)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
color = models.ForeignKey(Color, on_delete=models.CASCADE,blank=True,null=True)
size = models.ForeignKey(Size, on_delete=models.CASCADE,blank=True,null=True)
image_id = models.IntegerField(blank=True,null=True,default=0)
quantity = models.IntegerField(default=1)
price = models.DecimalField(max_digits=12, decimal_places=2,default=0)
def __str__(self):
return self.title
class ShopCart(models.Model):
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
variant = models.ForeignKey(Variants, on_delete=models.SET_NULL,blank=True, null=True) # relation with variant
quantity = models.IntegerField()
def __str__(self):
return self.product.title
views.py
def product_detail(request,id,slug):
query = request.GET.get('q')
current_user = request.user
category = Category.objects.all()
product = Product.objects.get(pk=id)
if product.variant != 'None':
variantid = request.POST.get('variantid') # from variant add to cart
checkinvariant = ShopCart.objects.filter(variant_id=variantid, user_id=current_user.id) # Check product in shopcart
if checkinvariant:
control = 1 # The product is in the cart
else:
control = 0 # The product is not in the cart"""
else:
checkinproduct = ShopCart.objects.filter(product_id=id, user_id=current_user.id) # Check product in shopcart
if checkinproduct:
control = 1 # The product is in the cart
else:
control = 0 # The product is not in the cart"""
if control == 1:
if product.variant == 'None':
tot = product.amount - ShopCart.objects.get(product_id=id).quantity
else:
variantid = request.POST.get('variantid')
tot = Variants.objects.get(pk=variantid).quantity - ShopCart.objects.get(variant_id=variantid).quantity
else:
if product.variant != 'None':
variantid = request.POST.get('variantid')
tot = Variants.objects.get(pk=variantid).quantity #error happens here
else:
tot = product.amount
Я просто хочу уменьшить количество товара при добавлении его в shopcart. Заранее спасибо!