如何为IntegerField设置NULL而不是设置0? [英] How to set NULL for IntegerField instead of setting 0?

查看:1320
本文介绍了如何为IntegerField设置NULL而不是设置0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



更改此项的最佳方法是什么?我试过这样做:

  age = models.IntegerField(blank = True,null = True)

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

解决方案

而不是使用

  age = models.IntegerField(blank = True,null =真的)

使用

  age = models.IntegerField()

当我们不指定

  blank = True,null = True 

它将在该列


中存储null

I'm uploading some data from an excel file using xlrd and turning that data into models (with mainly IntegerField values) in Django. My excel file has a bunch of missing data. Unfortunately, these missing data are converted to the value of 0 in my models, instead of NULL. This means that when my Person model doesn't have an age, that age gets recorded as 0, instead of as NULL.

What's the best way to change this? I've tried doing this:

age = models.IntegerField(blank=True, null=True)

But the blank fields still get set to 0 by default.

解决方案

Instead of using

age = models.IntegerField(blank=True, null=True)

use

age = models.IntegerField()

When we donot specify

blank=True, null=True

it will store null in that column

这篇关于如何为IntegerField设置NULL而不是设置0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆