title: trying to display a Sum on template of each instance of a class field filtered by another field on that same class. python django fantasy sports
body: <p>[screen shot of the fantasy team roster page]</p>
  <p>I am trying to build a fantasy sports website. I have NBA players (as a class) assigned to (django's built-in) Users (the fantasy team) via a field on the Player Class. I want to sum all of a User's players' salaries for a given year and list that on that User's (aka team's) Player roster page (regardless of who is signed in.) I was able to get this Sum on the django shell for a specific user_id:</p>
  <p>imported Player class to shell &amp; django.db.models import Sum
  Player.objects.filter(player_owner__exact='1').aggregate(Sum('player_sal_19_20'))</p>
  <p>result: {'player_sal_19_20__sum': Decimal('89654339')}</p>
  <p><strong>But how can I get this sum on the template for the fantasy team's public roster page?</strong></p>
  <p>I have spent days trying the aggregate function, trying to incorporate it with my get_queryset (see views snippet) function, template tags, and some other methods that went way over my head.</p>
  <p>I am using django's built-in Users as my teams and have created a player class on models.py:</p>
  <pre><code>class Player(models.Model):
    player_full = models.CharField(max_length=50)
    player_sal_19_20 = models.DecimalField(default=0, max_digits=10, decimal_places=2)
    player_sal_20_21 = models.DecimalField(default=0, max_digits=10, decimal_places=2)
    player_sal_21_22 = models.DecimalField(default=0, max_digits=10, decimal_places=2)
    player_sal_22_23 = models.DecimalField(default=0, max_digits=10, decimal_places=2)
    player_owner = models.ForeignKey(User, default='26', on_delete=models.CASCADE)
  </code></pre>
  <p>Then I created a view for a User's players page on views.py:</p>
  <pre><code>class UserPlayerListView(ListView):
    model = Player
    template_name = 'blog/user_players.html'
    context_object_name = 'players'
    paginate_by = 20
  
    def get_queryset(self):
        user = get_object_or_404(User, username=self.kwargs.get('username'))
        return Player.objects.filter(player_owner=user).order_by('-date_posted')
  </code></pre>
  <p>Finally I was able to map out the easy stuff on the html template with loops. But alas, I am at a road block when it comes to querying a a group of players' annual salary filtered by their assigned fantasy User.</p>
  <p>just to be clear, this is what i need:
  Sum the 'player_sal_19_20' field for each player that appears on a team's roster which is determined by the player's 'fantasy_owner' field.</p>
  <p>I am pretty new to django and python. I assume this is actually easy to do, so i apologize in advance if this is the case. Also, if anyone knows of examples of people using python/django for a fantasy sports site, that'd be helpful too!</p>