perf: 优化 queryset count

pull/12212/head
ibuler 2023-11-28 12:04:03 +08:00 committed by Eric_Lee
parent 73bed4d33d
commit 24987c7f60
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)