Opened 6 years ago

Closed 6 years ago

Last modified 4 years ago

#10408 closed (wontfix)

Invalid date in DB results in "ValueError: year is out of range" in admin view when using date_hierarchy

Reported by: iammichael Owned by: nobody
Component: Core (Other) Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

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 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

Attachments (1)

10408.diff (1.9 KB) - added by tclineks 6 years ago.

Download all attachments as: .zip

Change History (5)

comment:1 Changed 6 years ago by jacob

  • milestone set to 1.1
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Changed 6 years ago by tclineks

comment:2 Changed 6 years ago by tclineks

  • Has patch set

comment:3 Changed 6 years ago by jacob

  • Resolution set to wontfix
  • Status changed from new to closed

I'm going to change my mind and reject this. Django expects your data to be valid; if you've got invalid data, that's the problem.

comment:4 Changed 4 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

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