File tree Expand file tree Collapse file tree 2 files changed +40
-5
lines changed Expand file tree Collapse file tree 2 files changed +40
-5
lines changed Original file line number Diff line number Diff line change 1+ import rest_framework .exceptions
12import rest_framework .pagination
23import rest_framework .response
34
5+ import core .serializers
6+
47
58class CustomLimitOffsetPagination (
69 rest_framework .pagination .LimitOffsetPagination ,
@@ -9,12 +12,13 @@ class CustomLimitOffsetPagination(
912 max_limit = 100
1013
1114 def get_limit (self , request ):
12- raw_limit = request . query_params . get ( self . limit_query_param )
13-
14- if raw_limit is None :
15- return self . default_limit
15+ serializer = core . serializers . BaseLimitOffsetPaginationSerializer (
16+ data = request . query_params ,
17+ )
18+ serializer . is_valid ( raise_exception = True )
1619
17- limit = int (raw_limit )
20+ validated_data = serializer .validated_data
21+ limit = validated_data .get ('limit' , self .default_limit )
1822
1923 # Allow 0, otherwise cut by max_limit
2024 return 0 if limit == 0 else min (limit , self .max_limit )
Original file line number Diff line number Diff line change 1+ import rest_framework .exceptions
2+ import rest_framework .serializers
3+
4+
5+ class BaseLimitOffsetPaginationSerializer (
6+ rest_framework .serializers .Serializer ,
7+ ):
8+ """
9+ Base serializer for common filtering and sorting parameters.
10+ Pagination parameters (limit, offset) are handled by the pagination class.
11+ """
12+
13+ limit = rest_framework .serializers .IntegerField (
14+ min_value = 0 ,
15+ required = False ,
16+ )
17+ offset = rest_framework .serializers .IntegerField (
18+ min_value = 0 ,
19+ required = False ,
20+ )
21+
22+ def validate (self , attrs ):
23+ errors = {}
24+ for field in ('limit' , 'offset' ):
25+ raw = self .initial_data .get (field , None )
26+ if raw == '' :
27+ errors [field ] = ['This field cannot be an empty string.' ]
28+ if errors :
29+ raise rest_framework .exceptions .ValidationError (errors )
30+
31+ return super ().validate (attrs )
You can’t perform that action at this time.
0 commit comments