I have an Account class in Django with an interest_rate attribute (corresponding to account.interest_rate in my database). Here's the declaration for interest_rate:
interest_rate = models.FloatField(null=True, blank=True)
If I do something like this, it works:
account = Account()
account.interest_rate = 5
account.save()
But if I do this, it doesn't work:
account = Account()
account.interest_rate = None
account.save()
I get this error:
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
Why won't it let me save a null on a nullable field? Am I doing it wrong?