如何修复Django错误:“ “ unicode”对象没有属性“ tzinfo”在数据库读取

问题描述:

我正在Windows Azure上部署Django项目。我使用SQLite作为数据库服务器,一切正常。部署项目后,我决定将其与SQL Azure数据库连接,但似乎此解决方案产生了一些错误。我不再能够编辑用户个人资料。我总是得到这个错误:

I am deploying my Django project on Windows Azure. I was using SQLite as a database server and everything was ok. When I have deployed My project, I decided to connect it with an SQL Azure Database but it seems that this solution created some errors. I am no longer able to edit users profiles. I get always this error :


AttributeError at /admin/auth/user/1/
'unicode' object has no attribute 'tzinfo' 



当您的数据库包含这样的日期时间戳时,就会发生此错误:

This error happens when your database contains date-time stamps like this:

0000-00-00 00 :00:00.000000

(如果您使用MySQLWorkbench删除或覆盖以前的日期,则可能在MySQL中发生)

当您尝试在Django模型对象中检索这些记录时,您会从 pytz 时区库:

When you try to retrieve these records in a Django model object, you will get an exception from the pytz timezone library:

AttributeError 'unicode' object has no attribute 'tzinfo'

您应该先在数据库中编辑这些日期,并将其设置为最近的日期,例如 2018-01-01 00:00:00.000000 或设置为 NULL (但不能为空)。

You should edit these dates in your database first, and set them to more recent dates, like 2018-01-01 00:00:00.000000 or set to NULL (but not blank).

请参阅:

  • https://groups.google.com/forum/#!topic/django-users/Jg_9fQ3jMcU

另请参见:

  • #1292 - Incorrect date value: '0000-00-00'
  • Error in mysql when setting default value for DATE or DATETIME
  • How to store NULL values in datetime fields in MySQL?