python - 我想计算数据以在 django models.py 中连续保存

标签 python django-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)

最佳答案

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

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

blank 表示该字段允许为空。

检查 nullblank模型字段选项。

关于python - 我想计算数据以在 django models.py 中连续保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57704901/

相关文章:

python - 图形工具:通过用户定义的标签查找顶点

django - 为什么 django sqlite3 数据库在一台机器上与另一台机器上的工作方式不同?

python - Django:无法将模型中的用户配置文件数据插入模板中

python - key 错误 : 'locations' in Django app

python - Netbeans 不允许 Python 2.6 作为默认平台(强制使用 Jython2.5)

Python不转换日期

python - 从另一个 Python 脚本调用一个 Python 脚本,参数存储在字符串中

python - Flask-admin 如何在行旁边添加按钮

python - 如何在 django 中制作 db 转储文件

django - 使用列表过滤模型,然后使用另一个列表设置另一个字段