Code

Opened 6 years ago

Closed 2 years ago

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

Download all attachments as: .zip

Change History (13)

comment:1 Changed 6 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 6 years ago by oyvind

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

duplicate of #7147

comment:3 Changed 3 years ago by daniellindsley

  • milestone set to 1.3
  • Needs tests set
  • Resolution duplicate deleted
  • Status changed from closed to reopened

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 3 years ago by daniellindsley

Django 1.2.X version of the patch

Changed 3 years ago by daniellindsley

Django 1.3 version of the patch

comment:4 Changed 3 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 3 years ago by russellm

  • Resolution set to worksforme
  • Status changed from reopened to closed

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

comment:6 Changed 3 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 3 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

comment:8 Changed 2 years ago by anonymous

  • Easy pickings unset
  • Resolution worksforme deleted
  • Severity set to Normal
  • Status changed from closed to reopened
  • Type set to Uncategorized
  • UI/UX unset

is ind 1.4

comment:9 Changed 2 years ago by aaugustin

  • Resolution set to worksforme
  • Status changed from reopened to closed

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 21 months ago by simon29

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 21 months ago by simon29 (previous) (diff)

comment:11 Changed 19 months 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.