Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting related items in a Django template

Is it possible to sort a set of related items in a DJango template?

That is: this code (with HTML tags omitted for clarity):

{% for event in eventsCollection %}    {{ event.location }}    {% for attendee in event.attendee_set.all %}      {{ attendee.first_name }} {{ attendee.last_name }}    {% endfor %}  {% endfor %} 

displays almost exactly want I want. The only thing I want to change is I the list of attendees to be sorted by last name. I've tried saying something like this:

{% for event in events %}    {{ event.location }}    {% for attendee in event.attendee_set.order_by__last_name %}      {{ attendee.first_name }} {{ attendee.last_name }}    {% endfor %}  {% endfor %} 

Alas, the above syntax doesn't work (it produces an empty list) and neither does any other variation I have thought of (lot's of syntax errors reported, but no joy).

I could, of course, produce some kind of array of sorted attendee lists in my view, but that is an ugly and fragile (and did I mention ugly) solution.

Needless to say, but I'll say it anyway, I have perused the on-line docs and searched Stack Overflow and the archives of django-user without finding anything helpful (ah, if only a query set were a dictionary dictsort would do the job, but it's not and it doesn't)

==============================================

Edited to add additional thoughts after accepting Tawmas's answer.


Tawmas addressed the issue exactly as I presented it -- although the solution was not what I expected. As a result I learned a useful technique that can be used in other situations as well.

Tom's answer proposed an approach I had already mentioned in my OP and tentatively rejected as being "ugly".

The "ugly" was a gut reaction, and I wanted to clarify what was wrong with it. In doing so I realized that the reason it was an ugly approach was because I was hung up on the idea of passing a query set to the template to be rendered. If I relax that requirement, there is an un-ugly approach that should work.

I haven't tried this yet, but suppose that rather than passing the queryset, the view code iterated through the query set producing a list of Events, then decorated each Event with a query set for the corresponding attendees which WAS sorted (or filtered, or whatever) in the desired way. Something like so:

eventCollection = []    events = Event.object.[filtered and sorted to taste] for event in events:    event.attendee_list = event.attendee_set.[filtered and sorted to taste]    eventCollection.append(event) 

Now the template becomes:

{% for event in events %}    {{ event.location }}    {% for attendee in event.attendee_list %}      {{ attendee.first_name }} {{ attendee.last_name }}    {% endfor %}  {% endfor %} 

The downside is the view has to "actualize" all of the events at once which could be a problem if there were large numbers of events. Of course one could add pagination, but that complicates the view considerably.

The upside is the "prepare the data to be displayed" code is in the view where it belongs letting the template focus on formatting the data provided by the view for display. This is right and proper.

So my plan is to use Tawmas' technique for large tables and the above technique for small tables, with the definition of large and small left to the reader (grin.)

like image 286
Dale Wilson Avatar asked Jun 30 '11 19:06

Dale Wilson


People also ask

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.

What does {{ NAME }} this mean in Django templates?

What does {{ name }} this mean in Django Templates? {{ name }} will be the output. It will be displayed as name in HTML. The name will be replaced with values of Python variable.

What does filter do in Django template?

Django Template Engine provides filters which are used to transform the values of variables;es and tag arguments. We have already discussed major Django Template Tags. Tags can't modify value of a variable whereas filters can be used for incrementing value of a variable or modifying it to one's own need.


1 Answers

You can use template filter dictsort https://docs.djangoproject.com/en/dev/ref/templates/builtins/#std:templatefilter-dictsort

This should work:

{% for event in eventsCollection %}    {{ event.location }}    {% for attendee in event.attendee_set.all|dictsort:"last_name" %}      {{ attendee.first_name }} {{ attendee.last_name }}    {% endfor %}  {% endfor %} 
like image 100
tariwan Avatar answered Sep 26 '22 10:09

tariwan