From 4b869359fe0669aca6839b3a698115b52baebe74 Mon Sep 17 00:00:00 2001 From: Boluwatife Popoola Date: Sat, 18 Jan 2025 23:14:20 +0100 Subject: [PATCH 1/3] optimize list fetching query --- lists/api.py | 11 ++++++----- lists/serializers.py | 8 +------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/lists/api.py b/lists/api.py index 64745ce..930f5b2 100644 --- a/lists/api.py +++ b/lists/api.py @@ -10,6 +10,7 @@ OpenApiResponse, extend_schema, ) +from django.db.models import Count from rest_framework.request import Request from rest_framework.response import Response from rest_framework.views import APIView @@ -69,7 +70,7 @@ class ListsListAPI(APIView, CustomSizePageNumberPagination): ) @method_decorator(cache_page(60 * 1)) def get(self, request: Request, *args, **kwargs): - lists = List.objects.all().select_related("owner").prefetch_related("admins") + lists = List.objects.all().select_related("owner").prefetch_related("admins", "upvotes").annotate(registrations_count=Count('registrations')) account_id = request.query_params.get("account") if account_id: try: @@ -173,13 +174,13 @@ class ListRegistrationsAPI(APIView, CustomSizePageNumberPagination): 500: OpenApiResponse(description="Internal server error"), }, ) - @method_decorator(cache_page(60 * 1)) + # @method_decorator(cache_page(60 * 1)) def get(self, request: Request, *args, **kwargs): list_id = kwargs.get("list_id") - #list_obj = List.objects.prefetch_related('registrations').get(on_chain_id=list_id) - registrations = ListRegistration.objects.filter(list__on_chain_id=list_id).select_related("list__owner", "registrant", "registered_by").prefetch_related("list__admins") + # list_obj = List.objects.get(on_chain_id=list_id) + registrations = ListRegistration.objects.filter(list__on_chain_id=list_id).annotate(registrations_count=Count('list__registrations')).select_related("list", "list__owner", "registrant", "registered_by").prefetch_related("list__admins", "list__upvotes") - # registrations = list_obj.registrations.select_related().all() + # registrations = list_obj.registrations.select_related("list", "list__owner", "registrant", "registered_by").prefetch_related("list__admins").annotate(registrations_count=Count('list_registrations')).all() status_param = request.query_params.get("status") category_param = request.query_params.get("category") search_param = request.query_params.get("search") diff --git a/lists/serializers.py b/lists/serializers.py index 486abc6..c920c17 100644 --- a/lists/serializers.py +++ b/lists/serializers.py @@ -13,6 +13,7 @@ class Meta: class ListSerializer(ModelSerializer): + registrations_count = serializers.IntegerField(required=False) class Meta: model = List fields = [ @@ -34,10 +35,6 @@ class Meta: owner = AccountSerializer() admins = AccountSerializer(many=True) upvotes = ListUpvoteSerializer(many=True) - registrations_count = SerializerMethodField() - - def get_registrations_count(self, obj): - return obj.registrations.count() # def get_owner(self, obj): # return AccountSerializer(obj.owner).data @@ -51,7 +48,6 @@ class Meta: model = ListRegistration fields = [ "id", - "list", "registrant", "registered_by", "status", @@ -62,7 +58,6 @@ class Meta: "tx_hash", ] - list = ListSerializer() registrant = AccountSerializer() registered_by = AccountSerializer() @@ -104,7 +99,6 @@ class PaginatedListsResponseSerializer(serializers.Serializer): "registrant_notes": "I'm excited to apply for this list", "admin_notes": "This is a great project that I want on my list.", "tx_hash": "EVMQsXorrrxPLHfK9UnbzFUy1SVYWvc8hwSGQZs4RbTk", - "list": SIMPLE_LIST_EXAMPLE, "registrant": SIMPLE_ACCOUNT_EXAMPLE, "registered_by": SIMPLE_ACCOUNT_EXAMPLE, } From e6950123aba0131613f675ebe83c9abdedab6907 Mon Sep 17 00:00:00 2001 From: Boluwatife Popoola Date: Sat, 18 Jan 2025 23:15:21 +0100 Subject: [PATCH 2/3] add cache --- lists/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lists/api.py b/lists/api.py index 930f5b2..da2fc1f 100644 --- a/lists/api.py +++ b/lists/api.py @@ -174,7 +174,7 @@ class ListRegistrationsAPI(APIView, CustomSizePageNumberPagination): 500: OpenApiResponse(description="Internal server error"), }, ) - # @method_decorator(cache_page(60 * 1)) + @method_decorator(cache_page(60 * 1)) def get(self, request: Request, *args, **kwargs): list_id = kwargs.get("list_id") # list_obj = List.objects.get(on_chain_id=list_id) From c6c8b271c653c536e9fc93fda2087b72f2550bab Mon Sep 17 00:00:00 2001 From: Boluwatife Popoola Date: Sat, 18 Jan 2025 23:35:30 +0100 Subject: [PATCH 3/3] remove annotate --- lists/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lists/api.py b/lists/api.py index da2fc1f..b906d21 100644 --- a/lists/api.py +++ b/lists/api.py @@ -178,7 +178,7 @@ class ListRegistrationsAPI(APIView, CustomSizePageNumberPagination): def get(self, request: Request, *args, **kwargs): list_id = kwargs.get("list_id") # list_obj = List.objects.get(on_chain_id=list_id) - registrations = ListRegistration.objects.filter(list__on_chain_id=list_id).annotate(registrations_count=Count('list__registrations')).select_related("list", "list__owner", "registrant", "registered_by").prefetch_related("list__admins", "list__upvotes") + registrations = ListRegistration.objects.filter(list__on_chain_id=list_id).select_related("list", "list__owner", "registrant", "registered_by").prefetch_related("list__admins", "list__upvotes") # registrations = list_obj.registrations.select_related("list", "list__owner", "registrant", "registered_by").prefetch_related("list__admins").annotate(registrations_count=Count('list_registrations')).all() status_param = request.query_params.get("status")