From 46f413d8a5d7b750992d66a4a8924d16a39701e7 Mon Sep 17 00:00:00 2001 From: Somit Mittal Date: Mon, 2 Aug 2021 19:27:33 +0530 Subject: [PATCH] queryset.count() optimized --- graphene_django/fields.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/graphene_django/fields.py b/graphene_django/fields.py index e1972c7..8441d06 100644 --- a/graphene_django/fields.py +++ b/graphene_django/fields.py @@ -1,6 +1,7 @@ from functools import partial from django.db.models.query import QuerySet +from django.db.models import Count from graphql_relay.connection.arrayconnection import ( connection_from_array_slice, cursor_to_offset, @@ -144,7 +145,7 @@ class DjangoConnectionField(ConnectionField): iterable = maybe_queryset(iterable) if isinstance(iterable, QuerySet): - list_length = iterable.count() + list_length = iterable.aggregate(Count('pk'))['pk__count'] else: list_length = len(iterable) list_slice_length = (