2016-08-23 73 views
0


我的代碼存在一些問題。我看着必須解決它,但我沒有發現類似的問題。我想多個,然後總結一些字段值,但我從標題中得到錯誤。必須解決這個問題嗎?我想這是數據類型的問題,但我不知道如何定義方程。請幫幫我。Django錯誤:不支持的操作數類型爲*:'NoneType'和'float'

class Bill(models.Model): 
    date = models.DateField(default=datetime.now()) 
    tax = models.FloatField(default=0.20) 
    priceNoTax = models.IntegerField()#integer that I get from other class 
    priceTax = models.FloatField() 
    idAccount = models.ForeignKey(Account, on_delete=models.CASCADE, verbose_name="Account") 

    def save(self, *args, **kwargs): 
     if self.priceTax is None: 
      self.priceTax = self.priceNoTax+(self.priceNoTax*self.tax)#here is an error 
     super(Bill, self).save(*args, **kwargs) 

    def __str__(self): 
     return self.date 

非常感謝!

+1

您已檢查'priceTax'是否爲None,但您沒有檢查'priceNoTax'是否爲None,它顯然是。 –

回答

0

的參數在錯誤消息中的順序對應的代碼:

'NoneType' and 'float' < =>self.priceNoTax*self.tax

self.priceNoTax還沒有被初始化,或者如果加載此從數據庫然後它含有NULL。在乘法運算髮生之前,您可以通過self.priceNoTax的內容print驗證這一點。

仔細檢查如何設置self.priceNoTax

相關問題