Python Django can';t将序列乘以类型为'的非整数;str';

Python Django can';t将序列乘以类型为'的非整数;str';,python,django,Python,Django,在我的视图和模型中,我有这个逻辑,在我的视图中,我得到了用户输入的数据,在我的模型中,我自动计算了折扣价格公式,以及其他折扣价格公式,为什么我有这个错误无法将序列与类型为'str'的非整数相乘。。我如何解决这个问题 这是我的观点 otherdiscountpercentage = request.POST.get("otherdiscountpercentage") S_price = request.POST.get("price") otherdisc

在我的视图和模型中,我有这个逻辑,在我的视图中,我得到了用户输入的数据,在我的模型中,我自动计算了
折扣价格公式
,以及
其他折扣价格公式
,为什么我有这个错误<代码>无法将序列与类型为'str'的非整数相乘。。我如何解决这个问题

这是我的观点

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,
)
这是我的模特

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'

来自请求的值默认为
str
type,但Django直接进行转换。由于要覆盖
save
方法,因此需要将它们转换为int,如下所示:

otherdiscountpercentage = request.POST.get("otherdiscountpercentage")
if otherdiscountpercentage:
    otherdiscountpercentage = float(otherdiscountpercentage)

注意使用
request.POST.get('param_name')
意味着参数是可选的。因此,在进行任何计算之前,您应该设置一些条件或给出一个默认浮点值。

来自请求的值默认为
str
类型,但Django直接进行转换。由于要覆盖
save
方法,因此需要将它们转换为int,如下所示:

otherdiscountpercentage = request.POST.get("otherdiscountpercentage")
if otherdiscountpercentage:
    otherdiscountpercentage = float(otherdiscountpercentage)
注意使用
request.POST.get('param_name')
意味着参数是可选的。因此,在进行任何计算之前,您应该设置一些条件或给出一个默认浮点值。

您可以这样做

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=float(S_price),
    discount_percentage=float(discountpercentage),
    discount_price=float(discountprice),
    Other_discount_percentage=float(otherdiscountpercentage),
    Other_discount_price=float(otherdiscountprice),
)
你可以这样做

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=float(S_price),
    discount_percentage=float(discountpercentage),
    discount_price=float(discountprice),
    Other_discount_percentage=float(otherdiscountpercentage),
    Other_discount_price=float(otherdiscountprice),
)