perf: 优化 queryset count

pull/12213/head
ibuler 2023-11-28 12:04:03 +08:00 committed by Eric_Lee
parent 6b748e5ac5
commit 089cadeae3
1 changed files with 7 additions and 0 deletions

View File

@ -1,6 +1,13 @@
from django.conf import settings
from django.core.exceptions import FieldError
from rest_framework.pagination import LimitOffsetPagination
class MaxLimitOffsetPagination(LimitOffsetPagination):
max_limit = settings.MAX_LIMIT_PER_PAGE
def get_count(self, queryset):
try:
return queryset.values_list('id').order_by().count()
except (AttributeError, TypeError, FieldError):
return len(queryset)