Opened 15 years ago

Closed 15 years ago

#10313 closed (invalid)

TypeError coercing to Unicode on DateTime Field

Reported by: pvickers Owned by: nobody
Component: Uncategorized Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Karen Tracey)

The error I'm getting is
TypeError at /admin/dbasite/dbeventhistory/add/

coercing to Unicode: need string or buffer, datetime.datetime found

This occurs after doing a simple add using the automatically generated code by the Django admin interface.

I'm running Django 1.0, using cx_Oracle to connect to an Oracle database.
The cx_Oracle I have installed is version 5.0.1 but this exact same error also happens using version 4.4.
I have reproduced this error using Oracle 9i and Oracle 10g. On Oracle 10g, I tested using Oracle characterset WE8ISO8859P1 and UTF8 - the error still occurs.

I have a very simple model defined as:

class DBEventHistory(models.Model):
    event_date = models.DateTimeField()
    description = models.CharField(max_length=1024)
    dba = models.ForeignKey(DBA)

    def __unicode__(self):
        return self.event_date

    class Meta:
        db_table = u'DB_EVENT_HISTORY'
        verbose_name_plural = ('DB Event History')

The trace back I get is:


Request Method: POST
Request URL: http://cls049:8000/admin/dbasite/dbeventhistory/add/
Django Version: 1.0-final-SVN-unknown
Python Version: 2.4.3
Installed Applications:
Installed Middleware:

File "/usr/lib/python2.4/site-packages/django/core/handlers/" in get_response
  86.                 response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/" in root
  158.                 return self.model_page(request, *url.split('/', 2))
File "/usr/lib/python2.4/site-packages/django/views/decorators/" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/" in model_page
  177.         return admin_obj(request, rest_of_url)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/" in __call__
  191.             return self.add_view(request)
File "/usr/lib/python2.4/site-packages/django/db/" in _commit_on_success
  238.                 res = func(*args, **kw)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/" in add_view
  507.                 self.log_addition(request, new_object)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/" in log_addition
  294.             object_repr     = force_unicode(object), 
File "/usr/lib/python2.4/site-packages/django/utils/" in force_unicode
  49.                 s = unicode(s)

Exception Type: TypeError at /admin/dbasite/dbeventhistory/add/
Exception Value: coercing to Unicode: need string or buffer, datetime.datetime found

Change History (3)

comment:1 Changed 15 years ago by Karen Tracey

Description: modified (diff)

[Fixed formatting. Please use preview.]

I really don't think you want to be using cx_oracle

comment:2 in reply to:  1 Changed 15 years ago by Karen Tracey

Replying to kmtracey:

I really don't think you want to be using cx_oracle

Oops, cat got involved in typing there, didn't mean to hit submit. I was going to say I didn't think you want to be using cx_oracle 5.0, but perhaps 5.0.1 fixes the bug we'd had reported on that one earlier, so I'm not sure about that. You might want to make sure 5.0.1 fixes the bug referenced here: (And all of this is an aside to the actual problem reported.)

comment:3 Changed 15 years ago by Karen Tracey

Resolution: invalid
Status: newclosed

You need to change your __unicode__ method to:

    def __unicode__(self):
        return unicode(self.event_date)

[This is a Python requirement, nothing to do with Django. __unicode__ methods must return unicode (or, I guess, based on the message, string or buffer):

Python 2.5.1 (r251:54863, Jul 31 2008, 23:17:40) 
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class Thing(object):
...     def __init__(self, dt):
...         self.dt = dt
...     def __unicode__(self):
...         return self.dt
>>> import datetime
>>> x = Thing(
>>> unicode(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: coercing to Unicode: need string or buffer, datetime.datetime found
>>> unicode(x.dt)
u'2009-02-20 11:39:35.925831'
>>> class Thing2(object):
...    def __init__(self, dt):
...       self.dt = dt
...    def __unicode__(self):
...       return unicode(self.dt)
>>> x = Thing2(
>>> unicode(x)
u'2009-02-20 11:40:54.093501'

Returning x from a __unicode__ method where unicode(x) would turn x into unicode isn't sufficient, Python requires that the __unicode__ method do that explicitly.]

Note: See TracTickets for help on using tickets.
Back to Top