如何为IntegerField设置NULL而不是设置0? [英] How to set NULL for IntegerField instead of setting 0?
本文介绍了如何为IntegerField设置NULL而不是设置0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
更改此项的最佳方法是什么?我试过这样做:
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屋!
查看全文