TypeError: 不支持的操作数类型 *: ‘NoneType’ 和 ‘decimal.Decimal’

huangapple go评论137阅读模式
英文:

TypeError: unsupported operand type(s) for *: 'NoneType' and 'decimal.Decimal'

问题

以下是您要翻译的内容:

Following is the error:

File "F:\Coding Related\Karmachari-BackEnd\Karmachari_App\mainapp\models.py", line 97, in calculate_net_salary
    gross_pay = self.hours_worked * self.basic_pay_rate
                
TypeError: unsupported operand type(s) for *: 'NoneType' and 'decimal.Decimal'
These are the lasted error message in terminal.

models.py

class Payroll(models.Model):
    user = models.ForeignKey(User,on_delete=models.CASCADE)
    basic_pay_rate = models.DecimalField(max_digits=8, default=10000, decimal_places=2)
    overtime = models.DecimalField(max_digits=8,null=True, decimal_places=2)
    hours_worked = models.FloatField(null=True, blank=True)
    deductions = models.DecimalField(max_digits=8,null=True, decimal_places=2)
    net_pay = models.DecimalField(max_digits=8,null=True, blank=True, decimal_places=2)

    def calculate_net_salary(self):
        gross_pay = self.hours_worked * self.basic_pay_rate
        net_pay = gross_pay + self.overtime - self.deductions
        self.net_pay.save()

    # def save(self, *args, **kwargs):
    #     self.net_salary = self.calculate_net_salary()
    #     super(Payroll, self).save(*args, **kwargs)
    
    def __str__(self):
        return self.user.username

views.py

def payroll(request):
    payrolls = Payroll.objects.all()
    for payroll in payrolls:
        payroll.calculate_net_salary()
    user_object = User.objects.get(username=request.user.username)
    profile = Profile.objects.get(user=user_object)
    context={
        'profile':profile,
        'navbar':'salary',
        'payroll': payroll,
    }
    return render(request,'Salary_Sheet.html',context)

I want to access payroll of each employee and calculate net salary and provide them in pdf.
This is my first project so I dont know the concept clearly and want a beginner friendly explanation or solution.

英文:

Following is the error:

File "F:\Coding Related\Karmachari-BackEnd\Karmachari_App\mainapp\models.py", line 97, in calculate_net_salary
    gross_pay = self.hours_worked * self.basic_pay_rate
                
TypeError: unsupported operand type(s) for *: 'NoneType' and 'decimal.Decimal'
These are the lasted error message in terminal.

models.py

class Payroll(models.Model):
    user = models.ForeignKey(User,on_delete=models.CASCADE)
    basic_pay_rate = models.DecimalField(max_digits=8, default=10000, decimal_places=2)
    overtime = models.DecimalField(max_digits=8,null=True, decimal_places=2)
    hours_worked = models.FloatField(null=True, blank=True)
    deductions = models.DecimalField(max_digits=8,null=True, decimal_places=2)
    net_pay = models.DecimalField(max_digits=8,null=True, blank=True, decimal_places=2)

    def calculate_net_salary(self):
        gross_pay = self.hours_worked * self.basic_pay_rate
        net_pay = gross_pay + self.overtime - self.deductions
        self.net_pay.save()

    # def save(self, *args, **kwargs):
    #     self.net_salary = self.calculate_net_salary()
    #     super(Payroll, self).save(*args, **kwargs)
    
    def __str__(self):
        return self.user.username

views.py

def payroll(request):
    payrolls = Payroll.objects.all()
    for payroll in payrolls:
        payroll.calculate_net_salary()
    user_object = User.objects.get(username=request.user.username)
    profile = Profile.objects.get(user=user_object)
    context={
        'profile':profile,
        'navbar':'salary',
        'payroll': payroll,
    }
    return render(request,'Salary_Sheet.html',context)

I want to access payroll of each employee and calculate net salary and provide them in pdf.
This is my first project so I dont know the concept clearly and want a beginner friendly explanation or solution.

答案1

得分: 2

错误很明显:您试图将 NoneDecimal 相乘。

我猜测 self.hours_workedNone

解决此问题的一种简单方法是在这个值为 None 时使用零:

gross_pay = (self.hours_worked or 0) * self.basic_pay_rate

但我建议不要这样做,因为这会导致不必要的代码混乱。

更好的方法是不使用 null=True,而是在您的数值模型字段中设置 default=0。例如:

hours_worked = models.FloatField(default=0, blank=True)

您还需要重新应用迁移。

英文:

The error is very clear: You are trying to multiply a None and a Decimal.

I'm guessing self.hours_worked is None.

A dirty way to solve this issue would be to simply use zero when this value is None:

gross_pay = (self.hours_worked or 0) * self.basic_pay_rate

But I'd recommend not doing this as this leads to unnecessary code pollution.

Better way to do this is not to use null=True but, instead, set default=0 in your numeric model fields. Eg:

hours_worked = models.FloatField(default=0, blank=True)

You'll also need to reapply your migrations.

答案2

得分: 2

错误消息告诉您,您尝试在calculate_net_salary()方法中将None与十进制值相乘。这意味着在尝试计算gross_pay时,self.hours_worked属性为None。您可以在计算gross_pay之前添加一个检查,以查看self.hours_worked是否不是None

更好的方法是添加default=0,像这样:

hours_worked = models.FloatField(default=0, blank=True)
英文:

The error message is telling you that you are trying to multiply None with a Decimal value in your calculate_net_salary() method. This means that the self.hours_worked attribute is None when you are trying to calculate the gross_pay. You can add a check to see if self.hours_worked is not None before calculating gross_pay.

Better way to add default=0, like so:

hours_worked = models.FloatField(default=0, blank=True)

huangapple
  • 本文由 发表于 2023年3月7日 03:12:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654910.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定