Sorting¶
BaseSort contains two fields:
sort_by: the target field name, orNoneto leave the statement unchanged;sort_by_order:ascby default, ordesc.
from pydantic_filters import BaseSort, SortByOrder
sort = BaseSort(sort_by="created_at", sort_by_order=SortByOrder.desc)
For a public API, restrict sort_by to known values with an enum. This makes
invalid values fail Pydantic validation before they reach the driver:
from enum import Enum
from pydantic_filters import BaseSort
class UserSortField(str, Enum):
id = "id"
login = "login"
full_name = "full_name"
class UserSort(BaseSort):
sort_by: UserSortField | None = None
The SQLAlchemy driver raises
AttributeNotFoundSaDriverError
if sort_by does not name an attribute on the model. See the
SQLAlchemy guide for applying a sort model to a statement.