Prefetching Strategy
Overview
QuerySet Methods
List.objects.with_latest_actions()
def with_latest_actions(self):
"""
Prefetch the latest action for each list.
This enables the facts system by populating the `latest_actions` attribute,
which is checked by the `can_use_facts` property.
"""
return self.prefetch_related(
Prefetch(
"actions",
queryset=ListAction.objects.order_by(
"list_id", "-created", "-id"
).distinct("list_id"),
to_attr="latest_actions",
),
)List.objects.with_related_data()
List.objects.with_fighter_data()
ListFighter.objects.with_related_data()
ListFighterEquipmentAssignment.objects.with_related_data()
The can_use_facts Property
View Patterns
Multi-List Views (Campaigns, Homepage)
List Detail Views
Fighter Detail Views
The _prefetched_objects_cache Check
Performance Impact
Scenario
Without Prefetch
With Prefetch
Common Mistakes
1. Forgetting with_latest_actions()
2. Not using with_fighters for detail views
3. Double prefetching
See Also
Last updated