﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
10408	"Invalid date in DB results in ""ValueError: year is out of range"" in admin view when using date_hierarchy"	iammichael	nobody	"I have a legacy MySQL database that I'm trying to build a django application around.  When using a date_hierarcy in the admin interface, I receive a !ValueError when rendering the list of objects (full traceback in the test case, below).  This is caused by dates in the database that contain an invalid year ('0000').  The issue is similar to ticket:6642, but the problem is exhibited in the admin interface without any use of fixtures, dumpdata, or loaddata.  The root underlying cause, and thus the fix, may be the same for both, although the current patch in ticket:6642 updates only typecast_date and the error I am receiving is in typecast_timestamp.  The issue described here seems like it may be a little more common of an issue than the one described in ticket:6642.  


Test case:

Create a ""legacy"" mysql database using the mysql test case script [http://code.djangoproject.com/attachment/ticket/6642/test_case.mysql.sql attached to 6642].  

Setup the following model in models.py:
{{{
from django.db import models
class Testcase(models.Model):
    id = models.IntegerField(primary_key=True, unique=True, db_column='ID')
    date = models.DateField(db_column='Date')
    header = models.CharField(max_length=150, db_column='Header')
    class Meta:
	db_table = u'TestCase'
}}}

Enable the admin interface and add the following to admin.py
{{{
from django.contrib import admin
from models import Testcase

class TestcaseAdmin(admin.ModelAdmin):
    list_display = ('id', 'date', 'header')
    date_hierarchy = 'date'
admin.site.register(Testcase, TestcaseAdmin)
}}}

Now, try to view the Testcase data in the admin interface.  The processing of the date_hierarchy causes the following error:
{{{
TemplateSyntaxError at /admin/testcase/testcase/

Caught an exception while rendering: year is out of range

Original Traceback (most recent call last):
  File ""/usr/lib/python2.4/site-packages/django/template/debug.py"", line 71, in render_node
    result = node.render(context)
  File ""/usr/lib/python2.4/site-packages/django/template/__init__.py"", line 916, in render
    dict = func(*args)
  File ""/usr/lib/python2.4/site-packages/django/contrib/admin/templatetags/admin_list.py"", line 294, in date_hierarchy
    return {
  File ""/usr/lib/python2.4/site-packages/django/db/models/query.py"", line 186, in _result_iter
    self._fill_cache()
  File ""/usr/lib/python2.4/site-packages/django/db/models/query.py"", line 667, in _fill_cache
    self._result_cache.append(self._iter.next())
  File ""/usr/lib/python2.4/site-packages/django/db/models/sql/subqueries.py"", line 384, in results_iter
    date = typecast_timestamp(str(date))
  File ""/usr/lib/python2.4/site-packages/django/db/backends/util.py"", line 88, in typecast_timestamp
    int(times[0]), int(times[1]), int(seconds), int(float('.'+microseconds) * 1000000))
ValueError: year is out of range
}}}

"		closed	Core (Other)	1.0		wontfix			Accepted	1	0	0	0	0	0
