Skip to content

Commit 4f7ca62

Browse files
wmuldergovray-oxd
authored andcommitted
DBC22-5405: N+1 issue on border crossings
1 parent 75d4512 commit 4f7ca62

2 files changed

Lines changed: 3 additions & 4 deletions

File tree

src/backend/apps/border/serializers.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from apps.border.enums import LANE_TYPE
44
from apps.border.models import BorderCrossing, BorderCrossingLanes
5-
from django.db.models import Max
65
from rest_framework import serializers
76

87

@@ -49,8 +48,8 @@ class Meta:
4948
)
5049

5150
def get_last_updated(self, obj):
52-
last_updated = obj.bordercrossinglanes_set.aggregate(Max('last_updated'))['last_updated__max']
53-
return last_updated
51+
dates = [lane.last_updated for lane in obj.bordercrossinglanes_set.all() if lane.last_updated]
52+
return max(dates) if dates else None
5453

5554
def to_representation(self, instance):
5655
representation = super().to_representation(instance)

src/backend/apps/border/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
class BorderCrossingAPI(CachedListModelMixin):
9-
queryset = BorderCrossing.objects.all()
9+
queryset = BorderCrossing.objects.all().prefetch_related("bordercrossinglanes_set")
1010
serializer_class = BorderCrossingSerializer
1111
cache_key = CacheKey.BORDER_CROSSING_LIST
1212
cache_timeout = CacheTimeout.BORDER_CROSSING_LIST

0 commit comments

Comments
 (0)