如何为IntegerField设置NULL而不是设置0?

jyl*_*li7 17 python django xlrd django-models

我正在使用xlrd从excel文件上传一些数据,并将这些数据转换为Django中的模型(主要是IntegerField值).我的excel文件有一堆丢失的数据.不幸的是,这些丢失的数据在我的模型中转换为0,而不是NULL.这意味着当我的Person模型没有年龄时,该年龄将记录为0,而不是NULL.

改变这个的最好方法是什么?我试过这样做:

age = models.IntegerField(blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)

但是默认情况下,空白字段仍设置为0.

Shw*_*ran 16

而不是使用

age = models.IntegerField()
Run Code Online (Sandbox Code Playgroud)

使用

age = models.IntegerField(blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)

当我们指定

blank=True, null=True
Run Code Online (Sandbox Code Playgroud)

它将允许您在该列中存储null

  • 这是向后的 - 指定null = True*允许*NULL存储在列中. (6认同)