【发布时间】:2020-11-09 08:59:12
【问题描述】:
我的视图和模型中有这个逻辑,在我的视图中我得到用户输入的数据,在我的模型中,我有一个自动计算 discount_price_formula 和 other_discount_price_formula 为什么我有这个错误? can't multiply sequence by non-int of type 'str'。我该如何解决这个问题?
这是我的观点.py
otherdiscountpercentage = request.POST.get("otherdiscountpercentage")
S_price = request.POST.get("price")
otherdiscountprice = request.POST.get("otherdiscountprice")
discountpercentage = request.POST.get("discountpercentage")
discountprice = request.POST.get("discountprice")
insert_data = Product(
price=S_price,
discount_percentage=discountpercentage,
discount_price=discountprice,
Other_discount_percentage=otherdiscountpercentage,
Other_discount_price=otherdiscountprice,
)
这是我的models.py
class Product(models.Model):
price = models.FloatField(null=True, blank=True, verbose_name="Unit Price")
discount_percentage = models.FloatField(max_length=500, null=True, blank=True)
discount_price = models.FloatField(null=True, blank=True)
Other_discount_percentage = models.FloatField(null=True, blank=True)
Other_discount_price = models.FloatField(null=True, blank=True, default=0.0)
discount_price_formula = models.FloatField(null=True, blank=True)
other_discount_price_formula = models.FloatField(null=True, blank=True)
def save(self, *args, **kwargs):
self.discount_price_formula = self.price - (self.price * self.discount_percentage)
self.other_discount_price_formula = (self.price - (self.price * self.discount_percentage)) - ((self.price - (self.price * self.discount_percentage)) * self.Other_discount_percentage)
return super(Product, self).save(*args, **kwargs)
def __str__(self):
suser = '{0.product}'
return suser.format(self)
这是我的回溯
Traceback:
File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Desktop\onlinestoreapp\customAdmin\views.py" in InsertProduct
293. insert_data.save()
File "C:\Users\Desktop\onlinestoreapp\customAdmin\models.py" in save
209. self.discount_price_formula = self.price - (self.price * self.discount_percentage)
Exception Type: TypeError at /InsertProduct/
Exception Value: can't multiply sequence by non-int of type 'str'
【问题讨论】: