ユーザーのモデルの値の最も大きいもののみを表示させたいです。
views.pyに
@login_required def test(request): test_score = User.objects.filter(user=request.user).order_by('test_date') return render(request, 'test.html',{'test_score': test_score})
と書いて
models.pyに
class User(models.Model): user = models.ForeignKey("auth.User", verbose_name="imageforegin") test_date = models.IntegerField(max_length=100,null=True) score = models.IntegerField(max_length=100,null=True)
と、
test.htmlに
<h3>Your score is {% for score in test_score %} <h2 class="one"> {{ score.score }} </h2> {% endfor %} </h3>
と書きました。
テストの受験日が2017年03月03日なら、20170303とtest_dateに登録します。
他にテストの点数をscoreに登録(例えば60点なら60と)します。
ユーザー(user)・テストの受験日(test_date)・テストの点数(score)のデータは、同一ユーザーでも何種類もあります。例えば、
Tom 20170809 60
Tom 20170810 100
Tom 20171010 30
とデータがあるなら、test.htmlに
<h3>Your score is <h2 class="one">30</h2> </h3>
となるように表示させたいです。
views.pyの
test_score = User.objects.filter(user=request.user).order_by('test_date')
でtest_dateの最も新しいものを取って来ていると思ったのですが、今
<h3>Your score is <h2 class="score">60</h2> <h2 class="score">100</h2> <h2 class="score">30</h2> </h3>
のように全てのデータが表示されてしまっています。
なぜ1つだけデータをとって来ているつもりなのに、このように全部のデータをとって来てしまっているのでしょうか?
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。