Can I sort a set of related items in a DJango template?
That is: this code (with clear HTML tags):
{% for event in eventsCollection %} {{ event.location }} {% for attendee in event.attendee_set.all %} {{ attendee.first_name }} {{ attendee.last_name }} {% endfor %} {% endfor %}
displays almost exactly what I want. The only thing I want to change is the list of participants who will be sorted by last name. I tried to say 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 syntax above does not work (it creates an empty list), and no other variation that I thought of (a lot of syntax error messages, but without joy).
I could, of course, create some sort of array of sorted lists of visitors in my opinion, but this is an ugly and fragile (and I mentioned an ugly) solution.
Needless to say, but I still say that I looked through online documents and searched for qaru and django-user archives without finding anything useful (oh, if only the query set was a dictionary dictionary index, would do the job, but itβs not, and this is not so)
================================================
Edited to add additional thoughts after accepting Tawmas answer.
Taumas addressed the problem exactly as I presented it, although the solution was not what I expected. As a result, I learned a useful method that can be used in other situations.
Tom's answer suggested the approach I proposed in my OP and conditionally rejected as "ugly".
"Ugly" was a gut reaction, and I wanted to clarify what was wrong with her. At the same time, I realized that the reason this was an ugly approach was because I was hung up on the thought of transmitting the request given to the template that needs to be visualized. If I relax this requirement, there is no ugly approach that should work.
I have not tried this yet, but suppose that instead of sending a request, the presentation code is repeated through a set of requests, creating a list of events, and then arranges each event with a set of requests for the corresponding participants that were sorted (or filtered, or whatever ) in the desired way. Something like that:
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 disadvantage is that the view must βupdateβ all events at once, which can be a problem if there were a large number of events. Of course, you can add pagination, but this greatly complicates the presentation.
The surface is βprepare the data to be displayed,β the code is in the view where it belongs, allowing the template to focus on formatting the data provided by the view for display. This is right and right.
So, my plan is to use the Taumas technique for large tables and the technique described above for small tables, with the definition of large and small on the left of the reader (grin).