forked from erp-dev/erp
test: before all tests
This commit is contained in:
49
flower/viewsets.py
Normal file
49
flower/viewsets.py
Normal file
@@ -0,0 +1,49 @@
|
||||
"""
|
||||
自定义 ViewSet 基类和分页类
|
||||
|
||||
提供统一的分页限制,防止前端无限制请求大量数据。
|
||||
"""
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.pagination import LimitOffsetPagination
|
||||
|
||||
|
||||
class LimitedLimitOffsetPagination(LimitOffsetPagination):
|
||||
"""
|
||||
带最大限制的 LimitOffsetPagination
|
||||
|
||||
- 默认每页返回 100 条
|
||||
- 最大每页返回 100 条
|
||||
- 防止前端请求过多数据
|
||||
"""
|
||||
default_limit = 100
|
||||
max_limit = 100
|
||||
|
||||
|
||||
class LimitedModelViewSet(viewsets.ModelViewSet):
|
||||
"""
|
||||
带分页限制的 ModelViewSet 基类
|
||||
|
||||
自动应用 LimitedLimitOffsetPagination,限制每次最多返回 100 条数据。
|
||||
所有项目中的 ViewSet 应继承此类以保持一致的分页行为。
|
||||
"""
|
||||
pagination_class = LimitedLimitOffsetPagination
|
||||
|
||||
|
||||
class LimitedReadOnlyModelViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
"""
|
||||
带分页限制的 ReadOnlyModelViewSet 基类
|
||||
|
||||
自动应用 LimitedLimitOffsetPagination,限制每次最多返回 100 条数据。
|
||||
用于只读场景。
|
||||
"""
|
||||
pagination_class = LimitedLimitOffsetPagination
|
||||
|
||||
|
||||
class LimitedGenericViewSet(viewsets.GenericViewSet):
|
||||
"""
|
||||
带分页限制的 GenericViewSet 基类
|
||||
|
||||
自动应用 LimitedLimitOffsetPagination,限制每次最多返回 100 条数据。
|
||||
用于需要自定义 actions 的场景。
|
||||
"""
|
||||
pagination_class = LimitedLimitOffsetPagination
|
||||
Reference in New Issue
Block a user