From fd04e9fa773f1651650d680af9d397abfe54fb93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Sun, 18 Feb 2024 12:31:03 +0100 Subject: [PATCH] Sort prefetch instead of the result order_by on the final queryset results in duplicating editions, 1 for each purchase to fix it we sort the thing we actually want to sort - non-game purchases - in a prefetch earlier in the code --- games/views.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/games/views.py b/games/views.py index 22bca02..6cb8387 100644 --- a/games/views.py +++ b/games/views.py @@ -151,7 +151,9 @@ def view_game(request, game_id=None): game = Game.objects.get(id=game_id) nongame_related_purchases_prefetch = Prefetch( "related_purchases", - queryset=Purchase.objects.exclude(type=Purchase.GAME), + queryset=Purchase.objects.exclude(type=Purchase.GAME).order_by( + "date_purchased" + ), to_attr="nongame_related_purchases", ) game_purchases_prefetch = Prefetch( @@ -164,7 +166,7 @@ def view_game(request, game_id=None): editions = ( Edition.objects.filter(game=game) .prefetch_related(game_purchases_prefetch) - .order_by("year_released", "purchase__date_purchased") + .order_by("year_released") ) sessions = Session.objects.prefetch_related("device").filter(