#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: | 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)
Change History (13)
comment:1 by , 17 years ago
comment:3 by , 14 years ago
milestone: | → 1.3 |
---|---|
Needs tests: | set |
Resolution: | duplicate |
Status: | closed → 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.
comment:4 by , 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 , 14 years ago
Resolution: | → worksforme |
---|---|
Status: | reopened → closed |
Works for me; Provided test cases pass for me on trunk and 1.2.X.
comment:6 by , 14 years ago
Sorry for reopening. After updating & addressing a data problem, worksforme. Wish I could set the status to "embarassingwasteofcoredevtime".
comment:8 by , 12 years ago
Easy pickings: | unset |
---|---|
Resolution: | worksforme |
Severity: | → Normal |
Status: | closed → reopened |
Type: | → Uncategorized |
UI/UX: | unset |
is ind 1.4
comment:9 by , 12 years ago
Resolution: | → worksforme |
---|---|
Status: | reopened → 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 by , 12 years ago
If your database has nulls, make sure your model field knows about it. Just set null=True (or remove the nulls from the db).
comment:11 by , 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.
Confirmed, same here with r7512. Error disappearing after deleting the records which has null dates.