Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing up spell endpoint to fetch all needed data beforehand #592

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions api_v2/views/spell.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,23 @@ class SpellViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = serializers.SpellSerializer
filterset_class = SpellFilterSet

def get_queryset(self):
queryset = models.Spell.objects.all().order_by('pk')
depth = self.get_serializer().Meta.depth
queryset = SpellViewSet.setup_eager_loading(queryset, depth)
return queryset

@staticmethod
def setup_eager_loading(queryset, depth):
selects = ['document', 'school']
prefetches = ['classes', 'spellcastingoption_set']

if depth >= 1:
prefetches = prefetches + ['document__licenses']

if depth >= 2:
prefetches = prefetches + ['document__gamesystem', 'document__publisher']

queryset = queryset.select_related(*selects).prefetch_related(*prefetches)
return queryset

Loading