Sort prefetch instead of the result
Django CI/CD / test (push) Successful in 57s Details
Django CI/CD / build-and-push (push) Successful in 1m52s Details

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
This commit is contained in:
Lukáš Kucharczyk 2024-02-18 12:31:03 +01:00
parent 18902aedac
commit fd04e9fa77
Signed by: lukas
SSH Key Fingerprint: SHA256:vMuSwvwAvcT6htVAioMP7rzzwMQNi3roESyhv+nAxeg
1 changed files with 4 additions and 2 deletions

View File

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