0

I'm having trouble understanding how to relate additional information to an existing Django list and if I should be doing this in my templates or in the view somehow. Please see the below scenario:

class Team(models.Model):
    team_name = models.CharField(max_length=200)

For example in my view I am retrieving a list of all sports games and returning them:

def get_teams(request):

    teams = Team.objects.all()

    context = RequestContext(request, {
            'teams': teams,
        })
        return context

I would like to add some stats to the teams which I can then access in my template in the below fashion:

def get_teams(request):

    teams = Team.objects.all()

    for team in teams:
        team_win_percent(team)
        team_lose_percent(team)

    context = RequestContext(request, {
            'teams': teams,
        })
        return context


def team_win_percent(team)
    team_win_rate = [calculations here]

    return team_win_rate

def team_lose_percent(team)
    team_lose_rate = [calculations here]

    return team_lose_rate

What I am struggling to understand is how to add team_win_percentage and team_lose_percentage to my teams list so I can reference them in my template?

Any guidance much appreciated!

2 Answers 2

1

You could also for a few number of records append some field to the model query result the will be accessible from the template

class Team(models.Model): 
    team_name = models.CharField(max_length=200)


def get_teams(request):
    teams = Team.objects.all()
    for team in teams:
        team.team_win_percent = calculate_team_win(team)
        team.team_lose_percent = calculate_team_loss(team)
    ....

In template

{% for team in teams %}
    team win percentage = {{ team.team_win_percent }}
    team lose percentage = {{ team.team_lose_percent }}

{% endfor %}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much - this pointed me down the right path of what I was trying to do!
0

You have to write this as as a model method;

class Team(models.Model):
    team_name = models.CharField(max_length=200)

    def team_win_percent(self):
        #self = team object
        team_win_rate = [calculations here]

        return team_win_rate

    def team_lose_percent(self):
        #self = team object
        team_lose_rate = [calculations here]

        return team_lose_rate

In template:

{% for team in teams %}
    team win percentage = {{ team.team_win_percent }}
    team lose percentage = {{ team.team_lose_percent }}

{% endfor %}

3 Comments

Thanks for the response I might actually be calling those calculations from more than one place so am wondering if it is a good idea to avoid putting logic in the code for the database/models and if it was best practice to keep everything in the views?
Best practice is to keep logic in models
Serious question - what are views used for if logic is meant to be kept in models? Obviously templates are the user interface

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.