Question

This is an edit of an earlier question: I think regroup is what I'm looking for here but invite any suggestions on a different approach. I am trying to generate an html table for a scoring app for a multiplayer-backgammon game (called a chouette). A typical scoresheet would end up looking like this.

For current purposes, I've left out the "win/loss" column, which will be a calculated value, and I will worry later about how to get the Position and Score header to line up appropriate under the Player headers.

In the code below, I have attempted to use regroup to output rows showing the number of the game for the first column and then score and position for each player for that game. However, the output continues to generate new rows for each player for each game, as shown here. I have ordered by game in the view, so I don't understand why they're not grouping.

Models:

class Chouette(models.Model):
    date = models.DateTimeField()
    location = models.CharField(max_length=100)
    stake = models.IntegerField()

    def __unicode__(self) :
        return unicode(self.id)


class Position(models.Model):
    POSITION_CHOICES = (
    ('b', 'b'),
    ('c', 'c'),
    ('2', '2'),
    ('3', '3'),
    ('4', '4'),
    ('5', '5'),
    ('6', '6'),
    ('7', '7'),
    ('8', '8'),
    )
    position = models.CharField(max_length=10, choices=POSITION_CHOICES)

    def __unicode__(self) :
        return unicode(self.position)

class Player(models.Model):
    user = models.OneToOneField(User)
    handle = models.CharField(max_length=10)

    def __unicode__(self) :
        return unicode(self.handle)

class Game(models.Model):
    number = models.IntegerField()
    chouette = models.ForeignKey(Chouette)        

    def __unicode__(self) :
        return unicode(self.number)

class Score(models.Model):
    game = models.ForeignKey(Game)
    player = models.ForeignKey(Player)
    position = models.ForeignKey(Position)
    score = models.IntegerField()

View:

from django.shortcuts import render
from chouette.models import Score, Game

#chouette=1

def scores(request):
    p_list = Score.objects.filter(game__chouette=1).distinct('player').order_by('player')
    s_list = Score.objects.filter(game__chouette=1).order_by('game','player')
    return render(request, 'scores.html', {'player_list': p_list, 'score_list': s_list})

Template:

<!doctype html>
<html>
    <head></head>
    <body>
    <table>
        <tr>
        {% for player in player_list %}
            <th>{{ player.player }}</th>
        {% endfor %}
        </tr><tr>
        <th>Game</th>
        {% for player in player_list %}
            <th>Score</th><th>Position</th>
        {% endfor %}
        {% regroup score_list by game as list_by_game %}
            {% for game in list_by_game %}
            <tr><td>{{ game.grouper }}</td>
                    {% for g in game.list %}
                    <td>{{ g.score }}</td>
                    <td>{{ g.position }}</td>
                    {% endfor %}   
            {% endfor %}</tr>
        </table> 
    </body>
    </html>

Thanks for any help and for your patience with somebody new to Django.

Était-ce utile?

La solution

Problem was that the way the default form in Django admin was set up, it was creating a new game pk for every score; altered the form so that each game had only one pk.

Still not sure regroup is the best solution to my problem, as it forces me to insert null or empty records for players who have scores for some but not all games to get the other scores to display under the right column.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top