Django:从重组中访问数据

时间:2014-06-02 17:20:52

标签: django django-templates

我需要重现下表演示文稿。

enter image description here

我尝试过使用重新组合标记但是根据我的理解,我不能只有一个代表石斑鱼的字段。从截图中可以看出,我可以显示石斑鱼场(event_name)但是如何显示事件位置,总体位置和点值?

如果石斑鱼标签无法实现,我该如何实现?

感谢您的帮助。

html文件

{% regroup user_season_events by event_name as event_list %}

<div class="row" style="padding-top: 30px;">
    <div class="col-lg-12">
    <table class="footable table-design-1">
        <thead>
        <tr>
            <th data-class="expand" style="border-bottom: 2px solid silver;">Event</th>
            <th data-hide="phone" style="text-align:center; border-bottom: 2px solid silver;" data-type="numeric">Event position</th>
            <th data-hide="phone" style="text-align:center; border-bottom: 2px solid silver;" data-type="numeric">Overall position</th>
            <th width="100" style="text-align:center;" data-type="numeric">Points</th>
            <th style="border-bottom: 2px solid silver;" data-hide="phone,tablet" class="footable-sortable" data-sort-ignore="true">Votes</th>
        </tr>
        </thead>
        <tbody>
            {% for e in event_list %}
            <tr>
                <td style="text-align:center;">
                    <p style="text-align: center;">
                        {{ e.grouper }}
                    </p>
                    <p align="center">
                    </p>
                </td>
                <td style="text-align:center;">{{ e.grouper.event_position }}</td>
                <td style="text-align:center;">
                </td>
                <td class="totalpoints-number">
                </td>
                <td>
                    <div style="padding-top: 15px;">
                        {% for vote in e.list %}
                        <div style="padding-bottom:20px;">
                            <div class="row">
                                <div class="col-lg-9">
                                    {% if vote.valid_score == True and vote.event_status_id != 3 %}
                                        <span class="right-call">
                                    {% elif vote.valid_score == False and vote.event_status_id != 3 %}
                                        <span class="wrong-call">
                                    {% else %}
                                        <span class="no-call">
                                    {% endif %}
                                    {{ vote.fighter_name }}
                                    by <strong>{{ vote.howitended }}</strong>
                                    </span>
                                </div>
                                <div class="col-lg-3">
                                    {% if vote.valid_score == True and vote.event_status_id != 3 %}
                                        <span style="font-weight: bold;">
                                    {% elif vote.valid_score == False and vote.event_status_id != 3 %}
                                        <span>
                                    {% else %}
                                        <span>
                                    {% endif %}
                                        {{ vote.fightchoice_point }}</span>
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                        <div class="row">
                            <div class="col-lg-9"></div>
                            <div class="col-lg-2" style="border-top: 2px solid silver; padding-top:5px;">
                                {{ e.total_event_possible_points }}
                            </div>
                            <div class="col-lg-1"></div>
                        </div>
                    </div>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
    </div>
</div>

查看:

user_season_events = UserEvent.objects. \
        raw("SELECT users_userevent.event_id, users_userevent.id, fights_fightchoice.points, users_uservote.event_id, "
            "fights_fightchoice.howitended_id, fights_fightchoice.fighter_id, events_event.name as event_name, "
            "users_userevent.position as event_position, fighters_fighter.name as fighter_name, "
            "fights_howitended.name as howitended, fights_fightchoice.points as fightchoice_point, "
            "events_event.event_status_id, fights_fightchoice.valid_score, fights_fightchoice.id as "
            "fights_fightchoice_id, "
            "(SELECT SUM(fc.points) FROM users_uservote as uv, fights_fightchoice as fc "
            "WHERE uv.event_id=uv.event_id AND fc.id=uv.fightchoice_id  AND "
            "uv.user_id=" + str(request.user.id) + ")as total_event_possible_points "
            "FROM users_userevent "
            "INNER JOIN events_event ON ( users_userevent.event_id = events_event.id )"
            "INNER JOIN users_user ON ( users_userevent.user_id = users_user.id )"
            "LEFT OUTER JOIN users_uservote ON ( events_event.id = users_uservote.event_id ) "
            "LEFT OUTER JOIN fights_fightchoice ON ( users_uservote.fightchoice_id = fights_fightchoice.id ) "
            "INNER JOIN fighters_fighter ON fights_fightchoice.fighter_id = fighters_fighter.id "
            "INNER JOIN fights_howitended ON fights_fightchoice.howitended_id = fights_howitended.id "
            "WHERE (events_event.season_id = " + season_id + " AND users_userevent.user_id = " + str(request.user.id) +
            " AND users_uservote.user_id = " + str(request.user.id) + " )"
            "ORDER BY users_userevent.event_id DESC, events_event.event_on DESC, fights_fightchoice.id ASC   "
            )

    context_dict = {'user_season_events': user_season_events, 'user_seasons': user_seasons}

型号:

class UserVote(TimeStampedModel):
    event = models.ForeignKey('events.Event')
    fightchoice = models.ForeignKey('fights.FightChoice')
    user = models.ForeignKey('users.User')

class Event(TimeStampedModel):

    event_status = models.ForeignKey('events.EventStatus', null=True)
    event_type = models.ForeignKey('events.EventType', null=True)
    promotion = models.ForeignKey('promotions.Promotion', null=True)
    season = models.ForeignKey('seasons.Season', null=True)
    user = models.ForeignKey('users.User', null=True)

class UserEvent(TimeStampedModel):
    event = models.ForeignKey('events.Event')
    user = models.ForeignKey('users.User')

    points = models.IntegerField(blank=True, null=True)
    position = models.IntegerField(blank=True, null=True)
    previous_position = models.IntegerField(blank=True, null=True)
    total_participant = models.IntegerField(blank=True, null=True)
    total_position = models.IntegerField(blank=True, null=True)
    total_previous_position = models.IntegerField(blank=True, null=True)

我的sql结果:

enter image description here

2 个答案:

答案 0 :(得分:0)

回答一个问题是不礼貌的,但这里有:

我不知道您的型号,但您不能使用{{egrouper.position}}或{{egrouper.overall_position}}?

答案 1 :(得分:0)

自定义模板标记在这里会很有用:

@register.assignment_tag
def get_points_position(l):
    '''`l` is grouper list of dicts
    '''
    d = {'event_position': 0, 'points': 0}
    if l:
        # Assuming the event_position is always same in all fights?
        d['event_position'] = l[0]['event_position']

        # event points will be sum of all fights points?
        d['points'] = sum([f['points'] for f in l])
    return d

然后在模板中:

{% regroup user_season_events by event_name as event_list %}
{% for e in event_list %}
    {{ e.grouper }}
    {% get_points_position e.list as event_info %}
    {{ event_info.points }}
    {{ event_info.event_position }}
{% endfor %}