Opened 17 years ago

Closed 12 years ago

Last modified 12 years ago

#7145 closed Uncategorized (worksforme)

'NoneType' object has no attribute 'year' in admin change list when using date_hierarchy with a date field with null values

Reported by: Eric Walstad <eric@…> Owned by: nobody
Component: contrib.admin Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In changeset r7511

AttributeError
'NoneType' object has no attribute 'year'
Exception Type:  	AttributeError
Exception Value: 	'NoneType' object has no attribute 'year'
Exception Location: 	/usr/lib/python2.5/site-packages/django/contrib/admin/templatetags/admin_list.py in date_hierarchy, line 271

In the following, years is getting set to [None]. That seems to happen when there are null values in the date field, as best I can tell:

            years = cl.query_set.dates(field_name, 'year')
            return {
                'show': True,
                'choices': [{
                    'link': link({year_field: year.year}),
                    'title': year.year
                } for year in years]
            }

Local vars:

_[3] 	[]
cl 	<django.contrib.admin.views.main.ChangeList object at 0x89c628c>
day_field 	'date_paid__day'
day_lookup 	None
field_generic 	'date_paid__'
field_name 	'date_paid'
link 	<function <lambda> at 0x8b5af44>
month_day_format 	u'F j'
month_field 	'date_paid__month'
month_lookup 	None
year 	None
year_field 	'date_paid__year'
year_lookup 	None
year_month_format 	u'F Y'
years 	[None]

My temporary work around is to not use date_hierarchy in the admin ui.

Attachments (2)

7145_django_1.2.patch (6.3 KB ) - added by daniellindsley 14 years ago.
Django 1.2.X version of the patch
7145_django_1.3.patch (4.2 KB ) - added by daniellindsley 14 years ago.
Django 1.3 version of the patch

Download all attachments as: .zip

Change History (13)

comment:1 by Evren Esat Özkan <sleytr@…>, 17 years ago

Confirmed, same here with r7512. Error disappearing after deleting the records which has null dates.

comment:2 by oyvind, 17 years ago

Resolution: duplicate
Status: newclosed

duplicate of #7147

comment:3 by daniellindsley, 14 years ago

milestone: 1.3
Needs tests: set
Resolution: duplicate
Status: closedreopened

Sorry, but this was not a duplicate of #7147. It is still a bug as of Django 1.2.X.

Fixing it to omit null dates is easy but creating a test for it is proving difficult. I'll try to upload a patch shortly.

by daniellindsley, 14 years ago

Attachment: 7145_django_1.2.patch added

Django 1.2.X version of the patch

by daniellindsley, 14 years ago

Attachment: 7145_django_1.3.patch added

Django 1.3 version of the patch

comment:4 by daniellindsley, 14 years ago

Has patch: set
Needs tests: unset

I uploaded both a 1.2 & 1.3 version of the patch. However, the 1.3 is tests-only, as the same bug doesn't seem to be present in 1.3.

comment:5 by Russell Keith-Magee, 14 years ago

Resolution: worksforme
Status: reopenedclosed

Works for me; Provided test cases pass for me on trunk and 1.2.X.

comment:6 by daniellindsley, 14 years ago

Sorry for reopening. After updating & addressing a data problem, worksforme. Wish I could set the status to "embarassingwasteofcoredevtime".

comment:7 by Jacob, 13 years ago

milestone: 1.3

Milestone 1.3 deleted

comment:8 by anonymous, 12 years ago

Easy pickings: unset
Resolution: worksforme
Severity: Normal
Status: closedreopened
Type: Uncategorized
UI/UX: unset

is ind 1.4

comment:9 by Aymeric Augustin, 12 years ago

Resolution: worksforme
Status: reopenedclosed

Please don't reopen tickets anonymously without providing any details. If the problem still exists in 1.4, I recommend to file a new ticket with enough information for someone else to reproduce it. Thanks!

See the guidelines on reporting bugs for advice on what a good bug report looks like.

comment:10 by Simon Litchfield, 12 years ago

@daniellindsley- perhaps you could share the details of the "data problem" that causes the error?

Version 0, edited 12 years ago by Simon Litchfield (next)

comment:11 by dfraser@…, 12 years ago

I had this problem and the cause turned out to be a date of 0000-00-00 and thus an exception at

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py in date_hierarchy, line 350

occurred because of the None value at the head of the list of possible years. The relevant column in the table was defined as NOT NULL, so this is a MySQL related issue due to its defaults and the complexity resulting from them. Forcing a crash would be good because it makes one clean up the data, but a date of 0000-00-00 might be valid in some sense, so perhaps the code in admin_list.py should change?

I decided having a value of "unknown" was more appropriate given how the field was used, so I decided not to use the date_hierarchy feature.

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