我在 Django 中有一个 Account
类,它有一个 interest_rate
属性(对应于我数据库中的 account.interest_rate
)。以下是 interest_rate
的声明:
interest_rate = models.FloatField(null=True, blank=True)
如果我这样做,它会起作用:
account = Account()
account.interest_rate = 5
account.save()
但如果我这样做,它就不起作用了:
account = Account()
account.interest_rate = None
account.save()
我收到这个错误:
Traceback (most recent call last):
File "./import.py", line 18, in <module>
cProfile.run('g.process()', 'prof')
File "/usr/lib/python2.6/cProfile.py", line 29, in run
prof = prof.run(statement)
File "/usr/lib/python2.6/cProfile.py", line 135, in run
return self.runctx(cmd, dict, dict)
File "/usr/lib/python2.6/cProfile.py", line 140, in runctx
exec cmd in globals, locals
File "<string>", line 1, in <module>
File "/home/jason/projects/mcifdjango/mcif/models/generic_import.py", line 34, in process
Account.save_in_bulk(self.rows)
File "/home/jason/projects/mcifdjango/mcif/models/account.py", line 45, in save_in_bulk
cursor.execute(Account.bulk_insert_statement(rows))
File "/usr/lib/pymodules/python2.6/django/db/backends/util.py", line 15, in execute
return self.cursor.execute(sql, params)
File "/usr/lib/pymodules/python2.6/django/db/backends/mysql/base.py", line 86, in execute
return self.cursor.execute(query, args)
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 168, in execute
if not self._defer_warnings: self._warning_check()
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 82, in _warning_check
warn(w[-1], self.Warning, 3)
_mysql_exceptions.Warning: Data truncated for column 'interest_rate' at row 1
为什么它不允许我在可为 null 的字段上保存 null?我做错了吗?
最佳答案
mysql 中的字段是否可以为空?
我在过去创建一个模型时看到过这个错误,该模型的字段最初不允许为空,当创建表然后更改为允许它为空时。由于 mysql 在原始表创建时设置了 null = false,因此即使 django 允许它通过,它也会导致插入失败。
要解决这个问题,我会检查mysql中的表定义,如果该字段为null=false,您可以手动更改它,或者您可以删除表(如果您不关心数据),然后重新运行 ./manage.py syncdb 以重新创建表。
如果您使用 south 来管理您的架构迁移,您可以创建一个新的架构迁移,然后运行它。
如果mysql表实例没有显示null=false,那就是别的了。
更新: 我们发现 mysql 的列可以为空,所以我正在更新答案。
我使用的是 Django 1.2.5 版本,您使用的是哪个版本?
我在我的系统中创建了模型。
from django.db import models
#================================================================================
class Account(models.Model):
interest_rate = models.FloatField(null=True, blank=True)
然后通过 shell 运行它,它对我有用。
Python 2.6.6 (r266:84292, Sep 8 2010, 16:19:33)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from aggra.models import Account
>>> account = Account()
>>> account.interest_rate = None
>>> account.save()
>>> exit()
这是我在 django 中的创建语句。 aggra 是我的应用名称。
$ python manage.py sql aggra
BEGIN;
CREATE TABLE `aggra_account` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`interest_rate` double precision
)
;
COMMIT;
这是我的表在 mysql 中的样子。
CREATE TABLE `aggra_account` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`interest_rate` double DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
这看起来像你的吗?
更新 2 我们发现它看起来一样,并且使用的 django 和 python 版本大致相同。
你的mysql是什么版本?
你有什么SQL模式?我的是空的。
mysql> show variables where Variable_name = 'sql_mode';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sql_mode | |
+---------------+-------+
1 row in set (0.00 sec)
如果您将其设置为严格,则需要将其更改为更宽松。
http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html
另见。
http://forums.mysql.com/read.php?11,132672,132693#msg-132693
关于python - Django/Python 不喜欢 FloatField 的 None 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5476541/