How to fix this error ? “ 'unicode' object

2019-01-26 18:52发布

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'

2条回答
Melony?
2楼-- · 2019-01-26 19:34

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:

See also:

查看更多
Deceive 欺骗
3楼-- · 2019-01-26 19:55

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.

查看更多
登录 后发表回答