Opened 8 years ago

Closed 4 years ago

Last modified 4 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: master
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 6 years ago.
Django 1.2.X version of the patch
7145_django_1.3.patch (4.2 KB) - added by daniellindsley 6 years ago.
Django 1.3 version of the patch

Download all attachments as: .zip

Change History (13)

comment:1 Changed 8 years ago by Evren Esat Özkan <sleytr@…>

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

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

comment:2 Changed 8 years ago by oyvind

Resolution: duplicate
Status: newclosed

duplicate of #7147

comment:3 Changed 6 years ago by daniellindsley

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.

Changed 6 years ago by daniellindsley

Attachment: 7145_django_1.2.patch added

Django 1.2.X version of the patch

Changed 6 years ago by daniellindsley

Attachment: 7145_django_1.3.patch added

Django 1.3 version of the patch

comment:4 Changed 6 years ago by daniellindsley

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 Changed 6 years ago by Russell Keith-Magee

Resolution: worksforme
Status: reopenedclosed

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

comment:6 Changed 6 years ago by daniellindsley

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

comment:7 Changed 5 years ago by Jacob

milestone: 1.3

Milestone 1.3 deleted

comment:8 Changed 4 years ago by anonymous

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

is ind 1.4

comment:9 Changed 4 years ago by Aymeric Augustin

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 Changed 4 years ago by Simon Litchfield

If your database has nulls, make sure your model field knows about it. Just set null=True (or remove the nulls from the db).

Last edited 4 years ago by Simon Litchfield (previous) (diff)

comment:11 Changed 4 years ago by dfraser@…

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