【发布时间】:2013-12-22 09:02:45
【问题描述】:
我已经看到了所有类似的线程,阅读了文档,并尝试了许多组合以在 db 中将空值存储为 IntegerField 并且每次都失败。
我正在使用 MySQL。
我的models.py 定义了一个age=models.IntegerField() 字段。我从 csv 文件填充 db,有些单元格没有价值。 Django 文档说:
Field.null
If True, Django will store empty values as NULL in the database. Default is False.
Note that empty string values will always get stored as empty strings, not as NULL.
由于我正在使用IntegerField,我希望将一个空字符串(csv 中的一个空单元格)存储为 db 中的NULL。因此,我(认为)必须将null=True 添加到age 字段。实际上,我已经尝试了更多:
age=models.IntegerField()
age=models.IntegerField(null=True)
age=models.IntegerField(null=True, blank=True)
age=models.IntegerField(null=True, blank=True, default=None)
每次我向数据库插入一个空字符串时,我都会得到
ValueError: invalid literal for int() with base 10: ''
你猜我还能做什么?
【问题讨论】:
-
是的!也是我的问题!