Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7074089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:00:33+00:00 2026-05-28T06:00:33+00:00

I’m trying to display the Sum of amount_won for each user_name in the database.

  • 0

I’m trying to display the Sum of amount_won for each user_name in the database. My database is:

Stakes table

id
player_id
stakes
amount_won
last_play_date

Player table

id
user_name
real_name
site_played

models.py

class Player(models.Model):
    user_name = models.CharField(max_length=200)
    real_name = models.CharField(max_length=200)
    SITE_CHOICES = (
        ('FTP', 'Full Tilt Poker'),
        ('Stars', 'Pokerstars'),
        ('UB', 'Ultimate Bet'),
    )
    site_played = models.CharField(max_length=5, choices=SITE_CHOICES)
    def __unicode__(self):
        return self.user_name
    def was_created_today(self):
        return self.pub_date.date() == datetime.date.today()

class Stakes(models.Model):
    player = models.ForeignKey(Player)
    stakes = models.CharField(max_length=200)
    amount_won = models.DecimalField(max_digits=12, decimal_places=2)
    last_play_date = models.DateTimeField('Date Last Updated')
    def __unicode__(self):
        return self.stakes

class PlayerForm(ModelForm):
    class Meta:
        model = Player

class StakesForm(ModelForm):
    class Meta:
        model = Stakes

Views.py

def index(request):
    latest_player_list = Player.objects.all().order_by('id')[:20]
    total_amount_won = Stakes.objects.filter(player__user_name='test_username').aggregate(Sum('amount_won'))
    return render_to_response('stakeme/index.html', {
        'latest_player_list':     latest_player_list, 
        'total_amount_won': total_amount_won
     })

and index.html

<h1> Players </h1>

{% if latest_player_list %}
<ul>
{% for player in latest_player_list %}
    <li><a href="/stakeme/{{ player.id }}/">{{ player.user_name }} </a><br>Total Won: {{ total_amount_won }}
</li>
{% endfor %}
</ul>
<br>
{% else %}
<p>No players are available.</p>
{% endif %}

<h3><a href="/stakeme/new/">New Player</a></h3>

If I leave the views.py section as (player__user_name='test_username') it will display Amount Won: as follows Total Won: {'amount_won__sum': Decimal('4225.00')} using the test_username’s amount_won (4225.00) for EVERY user name. Ideally, I’d like it to display Amount Won: for each user name in the for loop and display it as “Amount Won: 4225.00” only.

I’m starting to understand this is way over my head, but I’ve read the docs regarding the differences between aggregate and annotate and I can’t wrap my head around it. I’m thinking my DB is not setup correctly to use annotate for this, but I obviously could be wrong.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:00:33+00:00Added an answer on May 28, 2026 at 6:00 am

    Check out: https://docs.djangoproject.com/en/dev/topics/db/aggregation/

    players = Player.objects.annotate(total_amount_won=Sum('stakes__amount_won'))
    
    players[0].total_amount_won # This will return the 'total amount won' for the 0th player
    

    So you could pass players to your template and loop over it.

    EDIT

    Your views.py would look like:

    def index(request):
        players = Player.objects.annotate(total_amount_won=Sum('stakes__amount_won'))
        return render_to_response('stakeme/index.html', {'players': players,})
    

    The template would look like:

    <h1> Players </h1>
    {% if players %}
    <ul>
    {% for player in players %}
    <li>
        <a href="/stakeme/{{ player.id }}/">{{ player.user_name }} </a><br>Total Won: {{ player.total_amount_won }}
    </li>
    {% endfor %}
    </ul>    
    <br />
    {% else %}
    <p>No players are available.</p>
    {% endif %}
    <h3><a href="/stakeme/new/">New Player</a></h3>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.