Python 我想计算要保存在django models.py中的一行数据

Python 我想计算要保存在django models.py中的一行数据,python,django-models,Python,Django Models,我有一个models类,它计算前一行以在下一行中添加值。即使我使用了null=True def BMICal(height, PatientWeight): Hiin = str(float(height)).split(".") heightInc = ((((int(Hiin[0]) * 12) + int(Hiin[1])) * 2.54) / 100) final = PatientWeight/(heightInc*heightInc) return

我有一个models类,它计算前一行以在下一行中添加值。即使我使用了
null=True


def BMICal(height, PatientWeight):
    Hiin = str(float(height)).split(".")
    heightInc = ((((int(Hiin[0]) * 12) + int(Hiin[1])) * 2.54) / 100)
    final = PatientWeight/(heightInc*heightInc)
    return final

class PatTest(models.Model):
    TestId = models.AutoField(primary_key=True)
    Patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
    BS = models.IntegerField()
    BP = models.IntegerField()
    PatHeight = models.FloatField()
    PatientWeight = models.PositiveIntegerField()
    BMI = models.FloatField(null=True)
    TEMPA = models.IntegerField()

    def clean(self):
        self.BMI = BMICal(self.PatHeight, self.PatientWeight)
        print(self.BMI)
    def __str__(self):
        return str(self.TestId)


您需要添加
blank=True
BMI=models.FloatField(null=True,blank=True)

null
将空值作为null存储在数据库中

blank
表示该字段允许为空


检查和模型字段选项。

我很高兴它有帮助:)