Refactor park queryset logic: move base queryset to a dedicated module for improved organization and maintainability

This commit is contained in:
pacnpal
2025-02-19 09:30:17 -05:00
parent 78f465b273
commit 5541a5f02d
3 changed files with 16 additions and 12 deletions

14
parks/querysets.py Normal file
View File

@@ -0,0 +1,14 @@
from django.db.models import QuerySet, Count, Q
from .models import Park
def get_base_park_queryset() -> QuerySet[Park]:
"""Get base queryset with all needed annotations and prefetches"""
return (
Park.objects.select_related('owner')
.prefetch_related('location', 'photos', 'rides')
.annotate(
current_ride_count=Count('rides', distinct=True),
current_coaster_count=Count('rides', filter=Q(rides__category="RC"), distinct=True)
)
.order_by('name')
)