在Django模板中排序相关项目 [英] Sorting related items in a Django template

查看:111
本文介绍了在Django模板中排序相关项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在DJango模板中对一组相关项目进行排序吗?

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

即:这段代码(为了清楚起见,省略了HTML标签):

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.

不用说,但我会说,无论如何,我已经阅读了在线文档,并搜索了Stack Overflow和django-user的档案,没有找到任何有用的东西(啊,如果只有一个查询集是一个字典dictsort会做这个工作,但不是,它不会)

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)

=========

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

编辑添加额外的想法
接受Tawmas的答案。

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

Tawmas正如我所提出的那样解决了这个问题它 - 虽然解决方案不是我的预期。因此,我学到了一种有用的技术,可以在其他情况下使用。

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.

汤姆的回答提出了我在OP中已经提到的一种方法,暂时被拒绝为丑陋。

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)

现在模板变成:

{% 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.

所以我的计划是使用Tawmas的大型表格技术和上面的小
表格的技术,大的定义

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.)

推荐答案

您需要在参加者模型中指定排序,就像这样。例如(假设你的模型类名为Attendee):

You need to specify the ordering in the attendee model, like this. For example (assuming your model class is named Attendee):

class Attendee(models.Model):
    class Meta:
        ordering = ['last_name']

请参阅手册供进一步参考。

修改即可。另一个解决方案是向事件模型添加一个属性,您可以从模板访问:

EDIT. Another solution is to add a property to your Event model, that you can access from your template:

class Event(models.Model):
# ...
@property
def sorted_attendee_set(self):
    return self.attendee_set.order_by('last_name')

您可以根据需要定义更多的这些...

You could define more of these as you need them...

这篇关于在Django模板中排序相关项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆