Opened 11 years ago

Closed 11 years ago

#4954 closed (wontfix)

sqlite3 time fields can fail typecasting

Reported by: anonymous Owned by: Adrian Holovaty
Component: Database layer (models, ORM) Version: master
Severity: Keywords: sqlite3 timefield
Cc: sql@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


My model (excerpt):

class TimeClock(models.Model):
    user = models.ForeignKey(User)
    day = models.DateField()
    start_time = models.TimeField()
    end_time = models.TimeField(blank=True, null=True)
    pause = models.IntegerField(help_text="In Minutes", blank=True, default=0)

The error:

Traceback (most recent call last):
File "/usr/lib/python2.5/site-packages/django/core/handlers/" in get_response
  77. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/views/" in _checklogin
  55. return view_func(request, *args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/views/decorators/" in _wrapped_view_func
  39. response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/views/" in change_list
  772. cl = ChangeList(request, model)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/views/" in __init__
  578. self.get_results(request)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/views/" in get_results
  636. result_list = list(self.query_set)
File "/usr/lib/python2.5/site-packages/django/db/models/" in __iter__
  113. return iter(self._get_data())
File "/usr/lib/python2.5/site-packages/django/db/models/" in _get_data
  480. self._result_cache = list(self.iterator())
File "/usr/lib/python2.5/site-packages/django/db/models/" in iterator
  188. cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params)
File "/usr/lib/python2.5/site-packages/django/db/backends/" in execute
  19. return self.cursor.execute(sql, params)
File "/usr/lib/python2.5/site-packages/django/db/backends/sqlite3/" in execute
  94. return Database.Cursor.execute(self, query, params)
File "/usr/lib/python2.5/site-packages/django/db/backends/" in typecast_time
  68. return datetime.time(int(hour), int(minutes), int(seconds), int(float('.'+microseconds) * 1000000))

  ValueError at /admin/timeclock/timeclock/
  invalid literal for int() with base 10: '2007-07-22 21'

It occurs viewing the admin list view, after inserting an object like this:
new = TimeClock(user=request.user,day=now,start_time=now )
where now =

In sqlite3 it looks like this:

sqlite> select * from timeclock_timeclock;
1|1|2007-07-22|2007-07-22 21:48:17.782227||0

Change History (1)

comment:1 Changed 11 years ago by Matt McClanahan <cardinal@…>

Resolution: wontfix
Status: newclosed
Summary: problems with sqlite3sqlite3 time fields can fail typecasting

This appears to be a sqlite issue stemming from storing date and time fields as strings. Other DB backends generally let you populate a time field with a date-time, but drop the date component. You can work around this issue by using instead of

Anything Django might attempt to do to work around this is error-prone. The sqlite backend could pattern match values that come out of time fields, trimming off bits that aren't a time string, but if a model with such a field were read from the DB and saved, the time field would be modified. While this might not be seen as a bad thing, it is at least questionable to passively sanitize bad data for the user.

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