Skip to content

Commit 7ac1e17

Browse files
authored
Merge pull request #330 from eadwinCode/disable_pagination_doc
Added docs on how to disable pagination
2 parents b9eea5c + 515db54 commit 7ac1e17

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

docs/api_controller/model_controller/02_model_configuration.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,64 @@ class EventModelController(ModelControllerBase):
206206
!!! info "Learn More About FilterSchema"
207207
For comprehensive documentation on FilterSchema features, custom expressions, combining filters, and advanced filtering techniques, visit: [https://django-ninja.dev/guides/input/filtering/](https://django-ninja.dev/guides/input/filtering/)
208208

209+
### **Disabling Pagination**
210+
211+
By default, Model Controllers apply pagination to the list endpoint. If you want to return all results without pagination, you can disable it by setting `pagination` to `None`:
212+
213+
```python
214+
@api_controller("/events")
215+
class EventModelController(ModelControllerBase):
216+
model_config = ModelConfig(
217+
model=Event,
218+
pagination=None, # Disables pagination
219+
)
220+
```
221+
222+
When pagination is disabled, the list endpoint will return all records in a single response:
223+
224+
```python
225+
# Response structure without pagination
226+
[
227+
{"id": 1, "title": "Event 1", ...},
228+
{"id": 2, "title": "Event 2", ...},
229+
{"id": 3, "title": "Event 3", ...},
230+
# ... all records
231+
]
232+
```
233+
234+
!!! warning "Performance Considerations"
235+
Disabling pagination can lead to performance issues and large response payloads if your model has many records. Use this option only when:
236+
237+
- You have a small, bounded dataset (e.g., less than 100 records)
238+
- You need to return all records for client-side processing
239+
- You're certain the dataset will remain small
240+
241+
For large datasets, consider using pagination with a reasonable page size instead.
242+
243+
You can also disable pagination while still using filtering:
244+
245+
```python
246+
from typing import Optional
247+
from ninja import FilterSchema
248+
249+
class EventFilterSchema(FilterSchema):
250+
title: Optional[str] = None
251+
category: Optional[str] = None
252+
253+
@api_controller("/events")
254+
class EventModelController(ModelControllerBase):
255+
model_config = ModelConfig(
256+
model=Event,
257+
pagination=None, # No pagination, but filtering is still available
258+
)
259+
260+
# Override list method to apply filtering manually
261+
def list(self, filters: EventFilterSchema):
262+
queryset = self.model_config.model.objects.all()
263+
queryset = filters.filter(queryset)
264+
return [self.model_config.retrieve_schema.from_orm(obj) for obj in queryset]
265+
```
266+
209267
## **Route Configuration**
210268

211269
You can customize individual route behavior using route info dictionaries. Each route type (`create_route_info`, `list_route_info`, `find_one_route_info`, `update_route_info`, `patch_route_info`, `delete_route_info`) accepts various configuration parameters.

0 commit comments

Comments
 (0)