Re: Accessing dictionaries in templates in a sorted manner

28 views
Skip to first unread message

Tom Evans

unread,
Jun 11, 2009, 5:09:29 AM6/11/09
to Django developers
On Wed, 2009-06-10 at 06:54 -0700, Adrian Rogers wrote:
> On Jun 9, 11:09 am, Tom Evans <tevans...@googlemail.com> wrote:
> > Hi all
> >
> > I was struggling to turn a typical template dictionary iteration use
> > case into a sorted dictionary iteration, and struggling to find any way
> > of managing it.
> >
> > Eg:
> >
> > {% for key, val in tdict.items %}....{% endfor %}
> >
> > After perusing docs, and the internet, it seemed like there was no
> > straightforward way to manage to turn this into a sorted dictionary
> > iteration, apart from altering the data structure outside the template.
> >
> > Finally, we looked at the source code for the dictsort filter, and it
> > seems like this would work to produce the desired result:
> >
> > {% for key, val in tdict.items|dictsort:"0" %}....{% endfor %}
> >
> > and, indeed, it does the trick.
> >
> > However, the documentation for dictsort indicates that it is for sorting
> > lists of dictionaries, and this behaviour seems to happen by
> > coincidence. If I use this construct, is it likely to fail in the
> > future, or is this actually intended usage?
> >
> > Cheers
> >
> > Tom
>
> That's really cool, I've been looking for something like that for ages
> and always had to tinker around with my data structures before putting
> them into the template. Anyone able to confirm if this is an intended
> use that I can rely on for the future?
>
> --
> Adrian
>

Can anyone comment on this? Sorry to bring it up in -developers, but
no-one will comment on it in -users.

Cheers

Tom

Alex Gaynor

unread,
Jun 11, 2009, 10:37:25 AM6/11/09
to django-d...@googlegroups.com
A lack of response on django-users is not a reason for sending something to -deveopers, this mailing listis strictly for the development of Django itself.

Alex

--
"I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire
"The people's good is the highest law."--Cicero

Tom Evans

unread,
Jun 11, 2009, 11:07:44 AM6/11/09
to django-d...@googlegroups.com
On Thu, 2009-06-11 at 09:37 -0500, Alex Gaynor wrote:
>
>
> On Thu, Jun 11, 2009 at 4:09 AM, Tom Evans <teva...@googlemail.com>

I appreciate that; I'm asking if this undocumented feature is expected
to work for the foreseeable future, and not likely to be changed in the
future development of django. This, in my mind, is pertinent to
django-developers.

Cheers

Tom

PS, this is hardly a difficult question, could you not have simply
answered it instead of being brusque?

Karen Tracey

unread,
Jun 11, 2009, 11:30:48 AM6/11/09
to django-d...@googlegroups.com
On Thu, Jun 11, 2009 at 11:07 AM, Tom Evans <tevans.uk@googlemail.com> wrote:

I appreciate that; I'm asking if this undocumented feature is expected
to work for the foreseeable future, and not likely to be changed in the
future development of django. This, in my mind, is pertinent to
django-developers.

Cheers

Tom

PS, this is hardly a difficult question, could you not have simply
answered it instead of being brusque?
 
Hardly a difficult question?  By your own phrasing you are asking about whether apparently undocumented behavior is likely to change in the future -- that doesn't strike me as a particularly easy question.  If I were to try to come up with an answer I'd have to look at the history of the code in question -- is it an area of code that has been changed a lot in the past, how stable is it lately, how many open tickets are there at the moment that might be likely to hit it?  How subtle is this undocumented effect -- is it something that a relatively minor change might cause a change in behavior or is it pretty well guaranteed by something fundamental in the way the code is presently implemented? Etc.

I have no idea what the answer to this question is.  All the information needed to answer those questions that I would look into to try to come up with an answer, though, is freely available in the svn history of the code and in trac.  So you are as likely as anyone (barring someone who knows offhand, which isn't likely given no answer on -users) to be able to come up with a reasonably accurate answer.

Karen

Tom Evans

unread,
Jun 11, 2009, 11:51:52 AM6/11/09
to django-d...@googlegroups.com
On Thu, 2009-06-11 at 11:30 -0400, Karen Tracey wrote:
> On Thu, Jun 11, 2009 at 11:07 AM, Tom Evans <teva...@googlemail.com>

Thanks, that actually *is* the answer: undocumented feature, do not
expect it to work in the long term.
I will come up with a different way to manage it - all I am trying to do
is write manageable code that doesn't require my knowledge to maintain.

Cheers

Tom

Guillem

unread,
Jun 11, 2009, 1:01:27 PM6/11/09
to Django developers
On Jun 11, 6:51 pm, Tom Evans <tevans...@googlemail.com> wrote:
> Thanks, that actually *is* the answer: undocumented feature, do not
> expect it to work in the long term.
> I will come up with a different way to manage it - all I am trying to do
> is write manageable code that doesn't require my knowledge to maintain.
>
> Cheers
>
> Tom

Just use a django.utils.datastructures.SortedDict.

M

Yuri Baburov

unread,
Jun 11, 2009, 5:48:40 PM6/11/09
to django-d...@googlegroups.com
Hi Tom,

SortedDict(yourdict) is a hard thing and takes a lot of tinkering?
don't you have python 2.4 to use sorted(dict.iteritems())?
can't write your own python function to do this in your views side?

next, |dictsort:"0" is working for at least two years and is pretty
stable functionality.
even if it will suddenly stop working -- you can take any dictsort
implementation and put it to your templatetags folder. is it not
acceptable?

next, documenting issue. please add a trac issue to document this
behavior, suggest your documentation. it's working well, people is
using it, it's listed at some recipes blogs, no important related bugs
found. why should one attempt to remove or change this feature?

you seems to be very curious about a merely meaningless question. of
course nobody knows about it a lot, nobody cares a lot, and of course
by the nature of group discussion nobody is answering.
--
Best regards, Yuri V. Baburov, ICQ# 99934676, Skype: yuri.baburov,
MSN: bu...@live.com

M. N. Islam Shihan

unread,
Jun 12, 2009, 12:12:49 AM6/12/09
to django-d...@googlegroups.com
There would be an easy work arround I think. As django support and
encourage custom django tags and filters in apps, you might want to
copy the existing code of current working dicsort filter into your own
app, rename it to something different than "dicsort" and use that
renamed filter in your templates without being worried about the
future breaking of builtin "dictsort" filter. This is what i typically
do, whenever I need to customize an implementation of builtin features
of django to meet my needs.

Still, I think the best place to get answer of this particular query
was at issue tracker system of django, instead of this developers
list, specially when the core developers are particularly busy with
the upcoming release of django.

Cheers
Shihan

Tom Evans

unread,
Jun 15, 2009, 6:37:33 AM6/15/09
to django-d...@googlegroups.com
On Fri, 2009-06-12 at 04:48 +0700, Yuri Baburov wrote:
> Hi Tom,
>
> SortedDict(yourdict) is a hard thing and takes a lot of tinkering?
> don't you have python 2.4 to use sorted(dict.iteritems())?
> can't write your own python function to do this in your views side?
>
> next, |dictsort:"0" is working for at least two years and is pretty
> stable functionality.
> even if it will suddenly stop working -- you can take any dictsort
> implementation and put it to your templatetags folder. is it not
> acceptable?
>
> next, documenting issue. please add a trac issue to document this
> behavior, suggest your documentation. it's working well, people is
> using it, it's listed at some recipes blogs, no important related bugs
> found. why should one attempt to remove or change this feature?
>
> you seems to be very curious about a merely meaningless question. of
> course nobody knows about it a lot, nobody cares a lot, and of course
> by the nature of group discussion nobody is answering.

Hi all (replying to all followups in one, to finish off the thread). The
main reason I didn't want to use SortedDict is that these structures
come from a complex data structure that a) doesn't know about a django,
and b) doesn't need to be sorted. It seemed wrong, and to break
encapsulation, to couple this module to django or to rework it's data
structures in order for it to be displayed in a template.

The suggestion to take the dictsort filter aside to ensure it continues
working is also a good one, thanks. I did consider raising a
documentation trac issue instead, saying it was an undocumented feature.
I actually thought this way would be less fuss, and less distracting
than a non-bug issue being raised during the 1.1 release.

To be honest, I'm surprised this caused as much fuss. I would have
thought it a pretty common use case, wanting to iterate through a
dictionary, sorted by it's keys. I'm surprised django has no way to do
this without using undocumented features.

Cheers

Tom

Reply all
Reply to author
Forward
0 new messages