[Django] #19963: date_hierarchy suddenly doesn’t support fields on related models

30 views
Skip to first unread message

Django

unread,
Mar 1, 2013, 5:38:23 PM3/1/13
to django-...@googlegroups.com
#19963: date_hierarchy suddenly doesn’t support fields on related models
-------------------------------+--------------------
Reporter: jbg@… | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 1.5
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
I'm not sure exactly when this changed, but I think it was in the last few
weeks of development leading up to the 1.5 release.

`ModelAdmin.date_hierarchy` used to support fields on related models. For
example, I have a model `Event` with a `day` field, which is a ForeignKey
to `Day`, which in turn has a `date` field (of type DateField).

This used to work:

{{{
class EventAdmin(ModelAdmin):
date_hierarchy = 'day__date'
}}}

Suddenly, as of a recent commit, it doesn't. This isn't addressed in the
documentation in any way I can find.

--
Ticket URL: <https://code.djangoproject.com/ticket/19963>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 1, 2013, 5:43:33 PM3/1/13
to django-...@googlegroups.com
#19963: date_hierarchy suddenly doesn’t support fields on related models
-------------------------------+--------------------------------------

Reporter: jbg@… | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 1.5
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by jbg@…):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

{{{
2013-03-02 11:34:28,310 while handling request
Traceback (most recent call last):
File "/home/.../django/django/db/models/options.py", line 341, in
get_field_by_name
return self._name_map[name]
KeyError: 'day__date'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/.../django/django/core/handlers/base.py", line 130, in
get_response
response = response.render()
File "/home/.../django/django/template/response.py", line 105, in render
self.content = self.rendered_content
File "/home/.../django/django/template/response.py", line 82, in
rendered_content
content = template.render(context)
File "/home/.../django/django/template/base.py", line 140, in render
return self._render(context)
File "/home/.../django/django/template/base.py", line 134, in _render
return self.nodelist.render(context)
File "/home/.../django/django/template/base.py", line 837, in render
bit = self.render_node(node, context)
File "/home/.../django/django/template/base.py", line 851, in
render_node
return node.render(context)
File "/home/.../django/django/template/loader_tags.py", line 123, in
render
return compiled_parent._render(context)
File "/home/.../django/django/template/base.py", line 134, in _render
return self.nodelist.render(context)
File "/home/.../django/django/template/base.py", line 837, in render
bit = self.render_node(node, context)
File "/home/.../django/django/template/base.py", line 851, in
render_node
return node.render(context)
File "/home/.../django/django/template/loader_tags.py", line 123, in
render
return compiled_parent._render(context)
File "/home/.../django/django/template/base.py", line 134, in _render
return self.nodelist.render(context)
File "/home/.../django/django/template/base.py", line 837, in render
bit = self.render_node(node, context)
File "/home/.../django/django/template/base.py", line 851, in
render_node
return node.render(context)
File "/home/.../django/django/template/loader_tags.py", line 62, in
render
result = block.nodelist.render(context)
File "/home/.../django/django/template/base.py", line 837, in render
bit = self.render_node(node, context)
File "/home/.../django/django/template/base.py", line 851, in
render_node
return node.render(context)
File "/home/.../django/django/template/loader_tags.py", line 62, in
render
result = block.nodelist.render(context)
File "/home/.../django/django/template/base.py", line 837, in render
bit = self.render_node(node, context)
File "/home/.../django/django/template/base.py", line 851, in
render_node
return node.render(context)
File "/home/.../django/django/template/base.py", line 1192, in render
_dict = func(*resolved_args, **resolved_kwargs)
File "/home/.../django/django/contrib/admin/templatetags/admin_list.py",
line 295, in date_hierarchy
field = cl.opts.get_field_by_name(field_name)[0]
File "/home/.../django/django/db/models/options.py", line 347, in
get_field_by_name
% (self.object_name, name))
django.db.models.fields.FieldDoesNotExist: Event has no field named
'day__date'
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/19963#comment:1>

Django

unread,
Mar 1, 2013, 5:50:32 PM3/1/13
to django-...@googlegroups.com
#19963: date_hierarchy suddenly doesn’t support fields on related models
-------------------------------+--------------------------------------

Reporter: jbg@… | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 1.5
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by jbg@…):

It looks like this commit did it:
https://github.com/django/django/commit/e74e207cce54802f897adcb42149440ee154821e

--
Ticket URL: <https://code.djangoproject.com/ticket/19963#comment:2>

Django

unread,
Mar 1, 2013, 5:54:18 PM3/1/13
to django-...@googlegroups.com
#19963: date_hierarchy suddenly doesn’t support fields on related models
-------------------------------+--------------------------------------

Reporter: jbg@… | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 1.5
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by aaugustin):

This commit isn't in 1.5, only in master. Indeed, it's backwards-
incompatible, as documented in the 1.6 release notes.

Are you sure the problem is in 1.5?

--
Ticket URL: <https://code.djangoproject.com/ticket/19963#comment:3>

Django

unread,
Mar 1, 2013, 5:57:20 PM3/1/13
to django-...@googlegroups.com
#19963: date_hierarchy suddenly doesn’t support fields on related models
-------------------------------+--------------------------------------

Reporter: jbg@… | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by jbg@…):

* version: 1.5 => master


Comment:

My apologies, I left the "Version" field at its default and was ambiguous
in my description. The commit was in the last few weeks of development
leading up to the 1.5 release, but you're correct, the problem exists only
in master.

I don't see anywhere in the 1.6 release notes where it says that
`date_hierarchy` no longer supports fields on related models.

--
Ticket URL: <https://code.djangoproject.com/ticket/19963#comment:4>

Django

unread,
Mar 1, 2013, 6:01:14 PM3/1/13
to django-...@googlegroups.com
#19963: date_hierarchy suddenly doesn’t support fields on related models
---------------------------------+------------------------------------

Reporter: jbg@… | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: master
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by aaugustin):

* severity: Normal => Release blocker
* stage: Unreviewed => Accepted


Comment:

Yeah, I was unclear, the commit involves some backwards incompatibility
but the problem you're reporting isn't part of them. I just wanted to
point out that the known incompatibilities were documented.

The bug is
[https://github.com/django/django/commit/e74e207cce54802f897adcb42149440ee154821e#L0R295
here]. Django now needs to determine if the target field is a date field
or a datetime field, and this line doesn't take into account relations.

--
Ticket URL: <https://code.djangoproject.com/ticket/19963#comment:5>

Django

unread,
Mar 1, 2013, 6:04:08 PM3/1/13
to django-...@googlegroups.com
#19963: date_hierarchy suddenly doesn’t support fields on related models
---------------------------------+-------------------------------------
Reporter: jbg@… | Owner: aaugustin
Type: Bug | Status: assigned

Component: contrib.admin | Version: master
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+-------------------------------------
Changes (by aaugustin):

* owner: nobody => aaugustin
* status: new => assigned


Comment:

I guess I'm supposed to fix my own mess...

--
Ticket URL: <https://code.djangoproject.com/ticket/19963#comment:6>

Django

unread,
Mar 13, 2013, 2:56:22 PM3/13/13
to django-...@googlegroups.com
#19963: date_hierarchy suddenly doesn’t support fields on related models
---------------------------------+-------------------------------------
Reporter: jbg@… | Owner: aaugustin
Type: Bug | Status: assigned
Component: contrib.admin | Version: master
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+-------------------------------------

Comment (by aaugustin):

I tried using `date_hierarchy` across a relation and it doesn't pass the
admin validation:

{{{
ImproperlyConfigured: 'ChildAdmin.date_hierarchy' refers to field
'parent__date' that is missing from model 'testapp.Child'.
}}}

Clearly it isn't a supported configuration. Validation is only performed
when `DEBUG = True`, but that doesn't make it a supported configuration
when `DEBUG = False`.

In fact the validation code is similar to the code I've added to look up
the field:
https://github.com/django/django/blob/master/django/contrib/admin/validation.py#L136-L142

I'm attaching a failing test case.

----

For the record, if Django started supporting date_hierarchy across
relations, the following patch would fix the problem described above:

{{{
--- a/django/contrib/admin/templatetags/admin_list.py
+++ b/django/contrib/admin/templatetags/admin_list.py
@@ -7,6 +7,7 @@ from django.contrib.admin.util import (lookup_field,
display_for_field,
from django.contrib.admin.views.main import (ALL_VAR,
EMPTY_CHANGELIST_VALUE,
ORDER_VAR, PAGE_VAR, SEARCH_VAR)
from django.contrib.admin.templatetags.admin_static import static
+from django.contrib.admin.utils import get_fields_from_path
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.utils import formats
@@ -292,7 +293,7 @@ def date_hierarchy(cl):
"""
if cl.date_hierarchy:
field_name = cl.date_hierarchy
- field = cl.opts.get_field_by_name(field_name)[0]
+ field = get_fields_from_path(cl.model, field_name)[-1]
dates_or_datetimes = 'datetimes' if isinstance(field,
models.DateTimeField) else 'dates'
year_field = '%s__year' % field_name
month_field = '%s__month' % field_name
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/19963#comment:7>

Django

unread,
Mar 13, 2013, 3:00:36 PM3/13/13
to django-...@googlegroups.com
#19963: Add support for date_hierarchy across relations
-------------------------------+------------------------------------
Reporter: jbg@… | Owner:
Type: New feature | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:

Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by aaugustin):

* owner: aaugustin =>
* status: assigned => new
* type: Bug => New feature
* severity: Release blocker => Normal


Comment:

Re-qualifying the ticket as a feature request — it seems useful to me.

Unless unexpected problems crop up, this means:
- upgrading the validation code,
- adding the validation tests I just uploaded,
- applying the diff I pasted above,
- adding tests,
- adding docs.

--
Ticket URL: <https://code.djangoproject.com/ticket/19963#comment:8>

Django

unread,
Mar 13, 2013, 3:52:10 PM3/13/13
to django-...@googlegroups.com
#19963: Add support for date_hierarchy across relations
-------------------------------+------------------------------------
Reporter: jbg@… | Owner:
Type: New feature | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by julien):

Aymeric, your plan sounds like the right way to go.

--
Ticket URL: <https://code.djangoproject.com/ticket/19963#comment:9>

Django

unread,
Sep 1, 2013, 10:46:24 PM9/1/13
to django-...@googlegroups.com
#19963: Add support for date_hierarchy across relations
-------------------------------+------------------------------------
Reporter: jbg@… | Owner:
Type: New feature | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by jbg@…):

This is purely a "practicality" suggestion, but given that this used to
work, and it doesn't look like it will work in 1.6 final, it may be worth
mentioning in the 1.6 release notes? Despite the fact that it was an
unsupported configuration and was not mentioned in the docs (as far as I
can tell), it worked fine, and if I'd been using it for months (after
assuming it would work, trying it out, and finding that it did work) then
I imagine other people might have been too.

So even just mentioning in the release notes, perhaps alongside the other
date-related backwards incompatibilities, that it no longer works in 1.6
might save a bunch of people a headache!

--
Ticket URL: <https://code.djangoproject.com/ticket/19963#comment:10>

Django

unread,
Aug 26, 2015, 2:37:57 AM8/26/15
to django-...@googlegroups.com
#19963: Add support for date_hierarchy across relations
-------------------------------+------------------------------------
Reporter: jbg@… | Owner:
Type: New feature | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by hobarrera):

* cc: hugo@… (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/19963#comment:11>

Django

unread,
Jan 27, 2016, 11:38:14 AM1/27/16
to django-...@googlegroups.com
#19963: Add support for date_hierarchy across relations
-------------------------------+------------------------------------
Reporter: jbg@… | Owner:
Type: New feature | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by eltismerino):

This ticket is fairly old. I've just discovered that it is still valid for
Django 1.8. Any plans on reimplementing the RelatedField/ForeignKey
feature for date_hierarchy?

Thanks

--
Ticket URL: <https://code.djangoproject.com/ticket/19963#comment:12>

Django

unread,
Jan 27, 2016, 12:27:11 PM1/27/16
to django-...@googlegroups.com
#19963: Add support for date_hierarchy across relations
-------------------------------+------------------------------------
Reporter: jbg@… | Owner:
Type: New feature | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by timgraham):

No, there aren't any plans outside of the discussion in this ticket, so
feel free to take ownership and provide a patch.

--
Ticket URL: <https://code.djangoproject.com/ticket/19963#comment:13>

Django

unread,
May 4, 2016, 6:20:26 PM5/4/16
to django-...@googlegroups.com
#19963: Add support for date_hierarchy across relations
-------------------------------+------------------------------------
Reporter: jbg@… | Owner: vytisb
Type: New feature | Status: assigned

Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by vytisb):

* status: new => assigned

* owner: => vytisb


--
Ticket URL: <https://code.djangoproject.com/ticket/19963#comment:14>

Django

unread,
May 5, 2016, 2:48:54 PM5/5/16
to django-...@googlegroups.com
#19963: Add support for date_hierarchy across relations
-------------------------------+------------------------------------
Reporter: jbg@… | Owner: vytisb
Type: New feature | Status: assigned
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by vytisb):

* has_patch: 0 => 1


Comment:

Submitted a PR: https://github.com/django/django/pull/6563

--
Ticket URL: <https://code.djangoproject.com/ticket/19963#comment:15>

Django

unread,
Jun 4, 2016, 12:23:52 PM6/4/16
to django-...@googlegroups.com
#19963: Add support for date_hierarchy across relations
-------------------------------+------------------------------------
Reporter: jbg@… | Owner: vytisb
Type: New feature | Status: closed
Component: contrib.admin | Version: master
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"2f9c4e2b6fab3ce08cfbebff79d758196250790c" 2f9c4e2b]:
{{{
#!CommitTicketReference repository=""
revision="2f9c4e2b6fab3ce08cfbebff79d758196250790c"
Fixed #19963 -- Added support for date_hierarchy across relations.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/19963#comment:16>

Reply all
Reply to author
Forward
0 new messages