Some optimizations to trade_offers to reduce loading times
This commit is contained in:
parent
0ac8ac8d5c
commit
9ce5d525b3
13 changed files with 255 additions and 222 deletions
|
|
@ -1,4 +1,6 @@
|
|||
from django.db import models
|
||||
from django.db.models import Prefetch
|
||||
from django.apps import apps
|
||||
|
||||
class DeckNameTranslation(models.Model):
|
||||
id = models.AutoField(primary_key=True)
|
||||
|
|
@ -55,6 +57,7 @@ class Deck(models.Model):
|
|||
|
||||
class Rarity(models.Model):
|
||||
id = models.AutoField(primary_key=True)
|
||||
normalized_id = models.IntegerField(null=False)
|
||||
name = models.CharField(max_length=64)
|
||||
icons = models.CharField(max_length=64)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
|
@ -63,14 +66,26 @@ class Rarity(models.Model):
|
|||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@property
|
||||
def normalized_id(self):
|
||||
"""
|
||||
For trading equivalence: treat Special Art Rare (pk 7) and Super Rare (pk 6) as the same.
|
||||
"""
|
||||
if self.pk in (6, 7):
|
||||
return 6
|
||||
return self.pk
|
||||
# Custom Manager for Card model
|
||||
class CardPrefetchManager(models.Manager):
|
||||
def get_queryset(self):
|
||||
return (
|
||||
super()
|
||||
.get_queryset()
|
||||
.select_related("cardset", "rarity")
|
||||
.prefetch_related(
|
||||
"decks",
|
||||
"decks__cardset",
|
||||
)
|
||||
)
|
||||
|
||||
class CardManager(models.Manager):
|
||||
def get_queryset(self):
|
||||
return (
|
||||
super()
|
||||
.get_queryset()
|
||||
.select_related("cardset", "rarity")
|
||||
)
|
||||
|
||||
class Card(models.Model):
|
||||
id = models.AutoField(primary_key=True)
|
||||
|
|
@ -82,6 +97,10 @@ class Card(models.Model):
|
|||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
# Use the custom manager to ensure optimized querysets everywhere.
|
||||
objects = CardPrefetchManager()
|
||||
objects_no_prefetch = CardManager()
|
||||
|
||||
def __str__(self):
|
||||
# For display, we show the original rarity icons.
|
||||
return f"{self.name} {self.rarity.icons} {self.cardset.name}"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue