1👍
✅
There are 2 problems.
- In your view,
top_5
is an unsorted list ofPlayer
objects.
top_5 = sorted(top_5, key=lambda player: player.player_points.latest().points, reverse=True)[:5] # Add this
return render(request, "points_app/monthlytop5players.html", {"results": top_5[:5]})
- In your test,
top_5
is a list (actuallyQuerySet
) ofPlayerPoint
objects.
results_pts = [player.player_points.latest() for player in test_results.context['results']] # Add this
for pt in top_5:
# self.assertIn(pt, test_results.context.get('results')) # Change this
self.assertIn(pt, results_pts) # to this
0👍
I think your problem is with this line:
latest_points = player.player_points.latest()
Specifically, latest()
. Like get(), earliest() and latest() raise DoesNotExist if there is no object with the given parameters.
You may need to add get_latest_by
to your model’s Meta
class. Maybe try this:
class PlayerPoint(models.Model):
...
class Meta:
get_latest_by = ['joined_ts']
If you don’t want to add this to your model, you could just do it directly:
latest_points = player.player_points.latest('-joined_ts')
if this is the problem.
Source:stackexchange.com