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

2024-09-27 21:26:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个models类,它计算前一行以在下一行中添加值。它总是显示错误消息“请选择下面的错误”即使我被使用null= True

Error Message


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)

Tags: selftruemodelsdef错误nullintbmi
1条回答
网友
1楼 · 发布于 2024-09-27 21:26:34

你需要添加blank=TrueBMI = models.FloatField(null=True, blank=True)

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

blank表示该字段允许为空

选中nullblank模型字段选项

相关问题 更多 >

    热门问题