简体   繁体   English

Django模板过滤器2个多对多字段

[英]Django template filters 2 ManytoMany fields

I am writing an application in Django that takes all of the athletes in an event, sorts them into categories (lightweight, heavyweight, etc), and then displays the athletes in each category sorted from the top scoring athlete to the bottom scoring athlete. 我正在Django中编写一个应用程序,该应用程序将一个事件中的所有运动员都进行分类,将他们分类(轻量级,重量级等),然后显示从最高得分运动员到最低得分运动员排序的每个类别的运动员。 I can't get my template to sort the athletes into classes; 我没有模板来将运动员分类。 either it displays all of the athletes or none. 它要么显示所有运动员,要么不显示。

Here are the relevant sections of my models and template: 这是我的模型和模板的相关部分:

An athlete may be in multiple categories and each category has multiple athletes. 一个运动员可能处于多个类别,每个类别都有多个运动员。 Also the categories are ordered by date. 类别也按日期排序。

models.py models.py

class Entry(models.Model):
    athlete = models.ForeignKey(Athlete, related_name='entries')
    event = models.ForeignKey(Event, related_name='entries')
    athlete_category = models.ManyToManyField(Category, related_name='entries')
    athlete_score = models.CharField(max_length=20, blank=True)
    placing = models.SmallIntegerField(blank=True, null=True)

class Category(models.Model):
    category_name = models.CharField(max_length=100)

class CategoryOrder(models.Model):
    event = models.ForeignKey(Event)
    category = models.ForeignKey(Classes)
    category_order = models.SmallIntegerField()

event_placings.html event_placings.html

{% for category in categories %}
    <p>{{ category.category_name }}</p>
        {% for entry in entries %}
            {% for athlete_category in entry.athlete_category %}
                {% if athlete_category == category %}
                    <p>{{ entry.athlete.first_name }} {{ entry.athlete.last_name }} - {{ entry.placing }}</p>
                {% endif %}
            {% endfor %}
        {% endfor %}
{% endfor %}

The template is supposed to list each of the categories and then all of the athletes in that category based on his placing. 该模板应该列出每个类别,然后根据其位置列出该类别中的所有运动员。 The output should be: 输出应为:

Men's Lightweight 男子轻量级
John Doe - 1 约翰·杜(John Doe)-1
Joe Public - 2 乔·大众-2

Women's Lightweight 女子轻量级
Jane Doe - 1 简·多伊-1
Eva Braun - 2 伊娃·布劳恩-2

etc. 等等

Currently I get: 目前我得到:

Men's Lightweight 男子轻量级

Women's Lightweight 女子轻量级

It lists the categories, but not the athletes. 它列出了类别,但没有列出运动员。 Where am I going wrong? 我要去哪里错了?

Looks like you have unnecessary loop in your template. 看起来您的模板中有不必要的循环。 It could be just like this: 可能是这样的:

{% for category in categories %}
    <p>{{ category.category_name }}</p>

    {% for entry in category.entries %}
        <p>{{ entry.athlete.first_name }} {{ entry.athlete.last_name }} - {{ entry.placing }}</p>
    {% endfor %}
{% endfor %}

To maintane ordering of athletes you can use Meta ordering on your Entry class or make method def athletes_ordered(self) on your Category class with something like 要维护运动员的排序,您可以在Entry类上使用Meta ordering ,也可以在Category类上使用以下方法使方法def athletes_ordered(self)类:

def athletes_ordered(self):
    return self.entries.order_by('athlete_score')

In the second case you'll have to replace category.entries by category.athletes_ordered in the template. 在第二种情况下,您必须用模板中的category.athletes_ordered替换category.entries

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM