Skip to content

Commit

Permalink
Support perPage query param
Browse files Browse the repository at this point in the history
  • Loading branch information
stevelacey authored and wadewilliams committed Oct 25, 2021
1 parent ee8a690 commit 0bf8ae4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,12 @@ Class Attributes
|`filter_fields` | no | `list` | _Default_: `None`. Pass a list of fields to support filtering via query params. |
|`search_fields` | no | `dict` or `bool` | _Default_: `None`. Pass a dict with `and`/`or` fields to search via the `q` query param. |
|`sort_fields` | no | `list` | _Default_: `None`. Pass a list of fields to support sorting via the `sort` query param. |
|`results_per_page` | no | `int` | _Default_: 25. Sets the number of results returned for each page. |
|`per_page` | no | `int` | _Default_: 25. Sets the number of results returned for each page. |
|`max_per_page` | no | `int` | _Default_: Same as `per_page`. Sets the max number of results to allow when passing the `perPage` query param. |
| | | | |

##### Search

Setting `search_fields` to `True` will enable search based on url parameters.
Parameters in the URL must be camelCase and exactly match the snake_case model
field.
Expand All @@ -196,7 +198,7 @@ must be composed of django filter lookup argument names.

All ListAPI views are paginated and include a `pagination` json object.

Use `results_per_page` to set custom limit for pagination. Default 25.
Use `per_page` to set custom limit for pagination. Default 25.

##### `get_queryset()` method
This method will use `lookup_url_kwarg` and `lookup_field` to filter results.
Expand Down
13 changes: 7 additions & 6 deletions worf/views/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


class ListAPI(AbstractBaseAPI):
results_per_page = 25
lookup_url_kwarg = "id" # default incase lookup_field is set
filters = {}
ordering = []
Expand All @@ -23,6 +22,8 @@ class ListAPI(AbstractBaseAPI):
filter_set = None
count = 0
page_num = 1
per_page = 25
max_per_page = None
num_pages = 1

def get(self, request, *args, **kwargs):
Expand Down Expand Up @@ -163,10 +164,10 @@ def paginated_results(self):
if settings.DEBUG:
self.query = str(queryset.query)

if self.results_per_page is None:
return queryset

paginator = Paginator(queryset, self.results_per_page)
default_per_page = getattr(self, "results_per_page", self.per_page)
per_page = max(int(request.GET.get("perPage") or default_per_page), 1)
max_per_page = self.max_per_page or default_per_page
paginator = Paginator(queryset, min(per_page, max_per_page))

self.page_num = int(request.GET.get("page") or request.GET.get("p") or 1)
if self.page_num < 1:
Expand Down Expand Up @@ -196,7 +197,7 @@ def serialize(self):
]
}

if self.results_per_page:
if self.per_page:
payload.update(
{
"pagination": dict(
Expand Down

0 comments on commit 0bf8ae4

Please sign in to comment.