Usage
ModelManager initialization¶
To use fastapi-sqlaclhemy-toolkit, you need to create an instance of ModelManager for your model:
from fastapi_sqlalchemy_toolkit import ModelManager
from .models import MyModel
from .schemas import MyModelCreateSchema, MyModelUpdateSchema
my_model_manager = ModelManager[MyModel, MyModelCreateSchema, MyModelUpdateSchema](MyModel)
The default_ordering attribute defines the default sorting when retrieving a list of objects. You should pass the primary model field to it.
from fastapi_sqlalchemy_toolkit import ModelManager
from .models import MyModel
from .schemas import MyModelCreateSchema, MyModelUpdateSchema
my_model_manager = ModelManager[MyModel, MyModelCreateSchema, MyModelUpdateSchema](
MyModel, default_ordering=MyModel.title
)
ModelManager methods¶
Below are the CRUD methods provided by ModelManager. Documentation for the parameters accepted by these methods can be found in the method docstrings.
create- creates an object; performs validation of field values at the database levelget- retrieves an objectget_or_404- retrieves an object or returns HTTP 404 errorexists- checks the existence of an objectpaginated_list/paginated_filter- retrieves a list of objects with filters and pagination throughfastapi_paginationlist/filter- retrieves a list of objects with filterscount- retrieves the count of objectsupdate- updates an object; performs validation of field values at the database leveldelete- deletes an object