【问题标题】:Django can't multiply sequence by non-int of type 'str'Django 不能将序列乘以“str”类型的非整数
【发布时间】:2020-11-09 08:59:12
【问题描述】:

我的视图和模型中有这个逻辑,在我的视图中我得到用户输入的数据,在我的模型中,我有一个自动计算 discount_price_formulaother_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'

【问题讨论】:

    标签: python django


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      你可以这样做

      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),
      )
      

      【讨论】:

        猜你喜欢
        • 2013-07-03
        • 2020-12-17
        • 2010-11-15
        • 1970-01-01
        • 2019-09-07
        • 2019-04-12
        • 1970-01-01
        • 2021-08-07
        • 1970-01-01
        相关资源
        最近更新 更多