Sunday, February 26, 2012

models.TimeField returning string rather than datetime.time with MySQL backend

i need to interact with an existing MySQL database table, so i built a
models.py file in a new app via introspection and then cleaned it up a
bit. For the TIME types, i am using models.TimeField, which maps to a
TIME type when using the MySQL backend.

Here's a sample from that models.py file:

http://dpaste.com/hold/708356/

What's strange is that when i'm accessing that `time_of_day` field,
it's returning as a string rather than a datetime.time:

>>> from reporting.models import FooModel
>>> m = FooModel.objects.latest()
>>> m.time_of_day
'09:19:00'

To be sure of the expected behavior, i set up a mock project with a
corresponding MySQL database on the same server. models.py:

http://dpaste.com/hold/708357/

Now, when i grab that `time` field, i get a datetime.time, which is
the expected behavior (note the final line):

>>> from timetest.models import Widget
>>> w = Widget.objects.latest()
>>> w.now
datetime.datetime(2012, 2, 25, 18, 45, 8)
>>> w.datetime
datetime.datetime(2012, 1, 1, 1, 23, 45)
>>> w.date
datetime.date(2012, 2, 2)
>>> w.time
datetime.time(12, 34, 56)

i haven't been able to explain the difference in behavior. The only
differences between the two databases and their respective tables that
i can think of are:
- the introspected database and tables weren't created by the Django
ORM
- the introspected tables are InnoDB and the Django-generated tables
are MyISAM
- introspected table row format are "compact" and Django-generated
row format are "fixed"

Using 1.3.X. Any idea what's going on?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment