Reduce /user/ queries with select and prefetch related
This commit is contained in:
parent
a28de294fa
commit
bc41a71219
|
@ -713,6 +713,7 @@ class UserSerializer(serializers.ModelSerializer):
|
||||||
training = UserTrainingSerializer(many=True)
|
training = UserTrainingSerializer(many=True)
|
||||||
member = MemberSerializer()
|
member = MemberSerializer()
|
||||||
transactions = serializers.SerializerMethodField()
|
transactions = serializers.SerializerMethodField()
|
||||||
|
training = serializers.SerializerMethodField()
|
||||||
interests = InterestSerializer(many=True)
|
interests = InterestSerializer(many=True)
|
||||||
door_code = serializers.SerializerMethodField()
|
door_code = serializers.SerializerMethodField()
|
||||||
wifi_pass = serializers.SerializerMethodField()
|
wifi_pass = serializers.SerializerMethodField()
|
||||||
|
@ -738,12 +739,27 @@ class UserSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
def get_transactions(self, obj):
|
def get_transactions(self, obj):
|
||||||
queryset = models.Transaction.objects.filter(user=obj)
|
queryset = models.Transaction.objects.filter(user=obj)
|
||||||
|
queryset = queryset.select_related('user', 'user__member')
|
||||||
queryset = queryset.exclude(category='Memberships:Fake Months')
|
queryset = queryset.exclude(category='Memberships:Fake Months')
|
||||||
queryset = queryset.order_by('-id', '-date')
|
queryset = queryset.order_by('-id', '-date')
|
||||||
serializer = TransactionSerializer(data=queryset, many=True)
|
serializer = TransactionSerializer(data=queryset, many=True)
|
||||||
serializer.is_valid()
|
serializer.is_valid()
|
||||||
return serializer.data
|
return serializer.data
|
||||||
|
|
||||||
|
def get_training(self, obj):
|
||||||
|
queryset = obj.training
|
||||||
|
queryset = queryset.select_related(
|
||||||
|
'session',
|
||||||
|
'session__course',
|
||||||
|
'session__instructor',
|
||||||
|
'session__instructor__member'
|
||||||
|
)
|
||||||
|
queryset = queryset.prefetch_related('session__students')
|
||||||
|
queryset = queryset.order_by('-id')
|
||||||
|
serializer = UserTrainingSerializer(data=queryset, many=True)
|
||||||
|
serializer.is_valid()
|
||||||
|
return serializer.data
|
||||||
|
|
||||||
def get_door_code(self, obj):
|
def get_door_code(self, obj):
|
||||||
if not obj.member.paused_date and obj.cards.count():
|
if not obj.member.paused_date and obj.cards.count():
|
||||||
return secrets.DOOR_CODE
|
return secrets.DOOR_CODE
|
||||||
|
|
Loading…
Reference in New Issue
Block a user