gpt4 book ai didi

python - 尝试通过 For 循环显示数据库中每个用户的 amount_won 总额

转载 作者:行者123 更新时间:2023-11-29 08:59:59 24 4
gpt4 key购买 nike

我正在尝试显示数据库中每个用户名的 amount_won 总和。我的数据库是:

赌注表

id
player_id
stakes
amount_won
last_play_date

玩家表

id
user_name
real_name
site_played

模型.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

View .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
})

和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>

如果我将views.py部分保留为(player__user_name='test_username'),它将显示赢得金额:如下赢得总计:{'amount_won__sum': Decimal('4225.00' )} 对每个用户名使用 test_username 的 amount_won (4225.00)。理想情况下,我希望它为 for 循环中的每个用户名显示 Amount Won: 并将其仅显示为“Amount Won: 4225.00”。

我开始明白这超出了我的理解范围,但我已经阅读了有关聚合和注释之间差异的文档,但我无法理解它。我认为我的数据库没有正确设置为此使用注释,但我显然可能是错的。

最佳答案

查看: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

因此您可以将players传递给您的模板并循环遍历它。

编辑

你的views.py看起来像:

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

模板看起来像:

<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>

关于python - 尝试通过 For 循环显示数据库中每个用户的 amount_won 总额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8936297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com