如何将小数字段除以浮点数?

时间:2018-11-21 09:38:45

标签: python django

这是我的模特

  class Assignment(models.Model):
        reiseziel = models.CharField(max_length=200, db_index=True)
        datum = models.DateField(auto_now=False)
        fahrzeug = models.ForeignKey(Car, related_name='car', on_delete=models.CASCADE, default='', blank=True, null=True)
        ausland = models.BooleanField(blank=True)
        gruppe = models.CharField(max_length=200, db_index=True)
        personen = models.IntegerField()
        reisepreis = models.DecimalField(default=0.00, max_digits=10, decimal_places=2)
        netto = models.DecimalField(default=0.00,max_digits=10,decimal_places=2)
        mwst = models.DecimalField(default=0.00, max_digits=10, decimal_places=2,)

        def __str__(self):
            return self.reiseziel

 def save(self, *args, **kwargs):
        self.netto = self.reisepreis / 1.19 
        self.mwst = self.reisepreis - self.netto
        super(Assignment, self).save(*args, **kwargs)

这是错误:

//:'decimal.Decimal'和'float'不受支持的操作数类型

1 个答案:

答案 0 :(得分:1)

您需要将两个值都转换为相同的类型以使除法有效。对于您而言,最可行的方法就是将float 1.19转换为Decimal

from decimal import Decimal

# (...)
self.netto = self.reisepreis / Decimal(1.19)