Paginated Module Reference¶
Deprecated
The fastcrud.paginated module is deprecated as of version 0.18.0 and will be removed in the next major version. Please import pagination utilities directly from fastcrud instead:
paginated is a utility module for offset pagination related functions. The functionality has been moved to the core module for better organization.
Core Pagination Module¶
The pagination utilities are now consolidated in a single core module:
Pagination Module¶
Pagination utilities for FastCRUD.
This module consolidates all pagination-related functionality including: - Pagination parameter schemas - Pagination response formatting - Offset calculation helpers - Dynamic response model creation
CursorPaginatedRequestQuery
¶
Bases: BaseModel
Pydantic model for cursor-based pagination query parameters.
This model encapsulates all query parameters used for cursor-based pagination in endpoints. It can be used with FastAPI's Depends() to inject these parameters into endpoints, making it easy to reuse in custom endpoints.
Cursor-based pagination is ideal for large datasets and infinite scrolling features, as it provides consistent results even when data is being modified.
Attributes:
| Name | Type | Description |
|---|---|---|
cursor |
Optional[int]
|
Cursor value for pagination (typically the ID of the last item from previous page) |
limit |
Optional[int]
|
Maximum number of items to return per page |
sort_column |
Optional[str]
|
Column name to sort by (defaults to 'id') |
sort_order |
Optional[str]
|
Sort order, either 'asc' or 'desc' (defaults to 'asc') |
Example
Source code in fastcrud/core/pagination.py
PaginatedRequestQuery
¶
Bases: BaseModel
Pydantic model for paginated query parameters.
This model encapsulates all query parameters used for pagination and sorting in read_items endpoints. It can be used with FastAPI's Depends() to inject these parameters into endpoints, making it easy to reuse in custom endpoints.
Supports two pagination modes: - Page-based: Using 'page' and 'items_per_page' (or 'itemsPerPage' alias) - Offset-based: Using 'offset' and 'limit'
Attributes:
| Name | Type | Description |
|---|---|---|
offset |
Optional[int]
|
Offset for unpaginated queries (used with limit) |
limit |
Optional[int]
|
Limit for unpaginated queries (used with offset) |
page |
Optional[int]
|
Page number for paginated queries |
items_per_page |
Optional[int]
|
Number of items per page for paginated queries |
sort |
Optional[str]
|
Sort results by one or more fields. Format: 'field1,-field2' where '-' prefix indicates descending order. Example: 'name' (ascending), '-age' (descending), 'name,-age' (name ascending, then age descending). |
Example
Source code in fastcrud/core/pagination.py
compute_offset(page, items_per_page)
¶
Calculate the offset for pagination based on the given page number and items per page.
The offset represents the starting point in a dataset for the items on a given page. For example, if each page displays 10 items and you want to display page 3, the offset will be 20, meaning the display should start with the 21st item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
page
|
int
|
The current page number. Page numbers should start from 1. |
required |
items_per_page
|
int
|
The number of items to be displayed on each page. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The calculated offset. |
Examples:
Source code in fastcrud/core/pagination.py
create_list_response(schema, response_key='data')
¶
Creates a dynamic ListResponse model with the specified response key.
Source code in fastcrud/core/pagination.py
create_paginated_response(schema, response_key='data')
¶
Creates a dynamic PaginatedResponse model with the specified response key.
Source code in fastcrud/core/pagination.py
paginated_response(crud_data, page, items_per_page, multi_response_key='data')
¶
Create a paginated response based on the provided data and pagination parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
crud_data
|
dict
|
Data to be paginated, including the list of items and total count. |
required |
page
|
int
|
Current page number. |
required |
items_per_page
|
int
|
Number of items per page. |
required |
multi_response_key
|
str
|
Key to use for the items list in the response (defaults to "data"). |
'data'
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A structured paginated response dict containing the list of items, total count, pagination flags, and numbers. |
Note
The function does not actually paginate the data but formats the response to indicate pagination metadata.
Source code in fastcrud/core/pagination.py
Backward Compatibility¶
For backward compatibility, the original imports still work but will issue deprecation warnings:
Backward compatibility module for paginated utilities.
.. deprecated:: 0.19.0
The fastcrud.paginated module is deprecated and will be removed in the next major version.
Please import pagination utilities directly from fastcrud instead:
.. code-block:: python
# Old (deprecated)
from fastcrud.paginated import PaginatedListResponse, PaginatedRequestQuery
# New (recommended)
from fastcrud import PaginatedListResponse, PaginatedRequestQuery