问题
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'
回答1:
I had same issue trying to use django-pyodbc-azure
database backend with Django (1.5.1): by default it stores DateTimeField
fields in your DB as datetime2(7)
, which looks to be still unsupported in Django.
In my case I added the option 'use_legacy_datetime' : True
in settings.py
, like below:
DATABASES = {
'default': {
'ENGINE' : 'sql_server.pyodbc',
'NAME' : '<MYDBNAME>',
'USER': '<MYDBUSER>',
'PASSWORD': '<MYDBPWD>',
'HOST': '<MYHOST>',
'OPTIONS': {
'use_mars': True,
'use_legacy_datetime' : True, # I added this line
},
},
}
I found my solution here.
I don't know Azure platform well, so I don't know if this is exactly your case, if not you can still modify your database replacing datetime2(N)
fields with good old datetime
ones.
Hope it helps.
回答2:
This error happens when your database contains date-time stamps like this:
0000-00-00 00:00:00.000000
(this can happen in MySQL if you delete or overwrite a previous date with MySQLWorkbench)
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'
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).
See:
- https://groups.google.com/forum/#!topic/django-users/Jg_9fQ3jMcU
See also:
- #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?
来源:https://stackoverflow.com/questions/16492031/how-to-fix-django-error-unicode-object-has-no-attribute-tzinfo-on-datab