Skip to content

CRUD Router API Reference

crud_router is a utility function for creating and configuring a FastAPI router with CRUD endpoints for a given model.

Function Definition

Creates and configures a FastAPI router with CRUD endpoints for a given model.

This utility function streamlines the process of setting up a router for CRUD operations, using a custom EndpointCreator if provided, and managing dependency injections as well as selective method inclusions or exclusions.

Parameters:

Name Type Description Default
session Callable

The SQLAlchemy async session.

required
model ModelType

The SQLAlchemy model.

required
create_schema Type[CreateSchemaType]

Pydantic schema for creating an item.

required
update_schema Type[UpdateSchemaType]

Pydantic schema for updating an item.

required
crud Optional[FastCRUD]

An optional FastCRUD instance. If not provided, uses FastCRUD(model).

None
delete_schema Optional[Type[DeleteSchemaType]]

Optional Pydantic schema for deleting an item.

None
path str

Base path for the CRUD endpoints.

''
tags Optional[list[Union[str, Enum]]]

Optional list of tags for grouping endpoints in the documentation.

None
include_in_schema bool

Whether to include the created endpoints in the OpenAPI schema.

True
create_deps Sequence[Callable]

Optional list of functions to be injected as dependencies for the create endpoint.

[]
read_deps Sequence[Callable]

Optional list of functions to be injected as dependencies for the read endpoint.

[]
read_multi_deps Sequence[Callable]

Optional list of functions to be injected as dependencies for the read multiple items endpoint.

[]
update_deps Sequence[Callable]

Optional list of functions to be injected as dependencies for the update endpoint.

[]
delete_deps Sequence[Callable]

Optional list of functions to be injected as dependencies for the delete endpoint.

[]
db_delete_deps Sequence[Callable]

Optional list of functions to be injected as dependencies for the hard delete endpoint.

[]
included_methods Optional[list[str]]

Optional list of CRUD methods to include. If None, all methods are included.

None
deleted_methods Optional[list[str]]

Optional list of CRUD methods to exclude.

None
endpoint_creator Optional[Type[EndpointCreator]]

Optional custom class derived from EndpointCreator for advanced customization.

None
is_deleted_column str

Optional column name to use for indicating a soft delete. Defaults to "is_deleted".

'is_deleted'
deleted_at_column str

Optional column name to use for storing the timestamp of a soft delete. Defaults to "deleted_at".

'deleted_at'
updated_at_column str

Optional column name to use for storing the timestamp of an update. Defaults to "updated_at".

'updated_at'
endpoint_names Optional[dict[str, str]]

Optional dictionary to customize endpoint names for CRUD operations. Keys are operation types ("create", "read", "update", "delete", "db_delete", "read_multi"), and values are the custom names to use. Unspecified operations will use default names.

None
filter_config Optional[Union[FilterConfig, dict]]

Optional FilterConfig instance or dictionary to configure filters for the read_multi endpoint.

None

Returns:

Type Description
APIRouter

Configured APIRouter instance with the CRUD endpoints.

Raises:

Type Description
ValueError

If both included_methods and deleted_methods are provided.

Examples:

Models and Schemas Used Below
mymodel/model.py
from sqlalchemy import Boolean, Column, DateTime, Integer, String
from sqlalchemy.orm import DeclarativeBase


class Base(DeclarativeBase):
    pass


class MyModel(Base):
    __tablename__ = "my_model"
    id = Column(Integer, primary_key=True)
    name = Column(String)
mymodel/schemas.py
import datetime

from pydantic import BaseModel


class CreateMyModelSchema(BaseModel):
    name: str | None = None


class UpdateMyModelSchema(BaseModel):
    name: str | None = None


class DeleteMyModelSchema(BaseModel):
    pass

customer/model.py
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import DeclarativeBase


class Base(DeclarativeBase):
    pass


class Customer(Base):
    __tablename__ = "customer"
    id = Column(Integer, primary_key=True)
    name = Column(String)
customer/schemas.py
from pydantic import BaseModel


class CreateCustomerSchema(BaseModel):
    name: str | None = None


class ReadCustomerSchema(BaseModel):
    id: int
    name: str | None = None


class UpdateCustomerSchema(BaseModel):
    name: str | None = None


class DeleteCustomerSchema(BaseModel):
    pass
product/model.py
from sqlalchemy import Column, Integer, Numeric, String
from sqlalchemy.orm import DeclarativeBase


class Base(DeclarativeBase):
    pass


class Product(Base):
    __tablename__ = "product"
    id = Column(Integer, primary_key=True)
    name = Column(String)
    price = Column(Numeric)
product/schemas.py
from pydantic import BaseModel


class CreateProductSchema(BaseModel):
    name: str | None = None
    price: float | None = None


class ReadProductSchema(BaseModel):
    id: int
    name: str | None = None
    price: float | None = None


class UpdateProductSchema(BaseModel):
    name: str | None = None
    price: float | None = None


class DeleteProductSchema(BaseModel):
    pass
order/model.py
from sqlalchemy import Column, ForeignKey, Integer
from sqlalchemy.orm import DeclarativeBase


class Base(DeclarativeBase):
    pass


class Order(Base):
    __tablename__ = "order"
    id = Column(Integer, primary_key=True)
    customer_id = Column(Integer, ForeignKey("customer.id"))
    product_id = Column(Integer, ForeignKey("product.id"))
    quantity = Column(Integer)
order/schemas.py
from pydantic import BaseModel


class CreateOrderSchema(BaseModel):
    customer_id: int | None = None
    product_id: int | None = None
    quantity: int | None = None


class ReadOrderSchema(BaseModel):
    id: int
    customer_id: int | None = None
    product_id: int | None = None
    quantity: int | None = None


class UpdateOrderSchema(BaseModel):
    customer_id: int | None = None
    product_id: int | None = None
    quantity: int | None = None


class DeleteOrderSchema(BaseModel):
    pass

tier/model.py
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import DeclarativeBase


class Base(DeclarativeBase):
    pass


class Tier(Base):
    __tablename__ = "tier"
    id = Column(Integer, primary_key=True)
    name = Column(String, unique=True)
tier/schemas.py
from pydantic import BaseModel


class ReadTierSchema(BaseModel):
    id: int
    name: str | None = None
department/model.py
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import DeclarativeBase


class Base(DeclarativeBase):
    pass


class Department(Base):
    __tablename__ = "department"
    id = Column(Integer, primary_key=True)
    name = Column(String)
department/schemas.py
from pydantic import BaseModel


class ReadDepartmentSchema(BaseModel):
    id: int
    name: str | None = None
user/model.py
from sqlalchemy import (
    Boolean,
    Column,
    DateTime,
    ForeignKey,
    Integer,
    String,
    func,
)
from sqlalchemy.orm import DeclarativeBase


class Base(DeclarativeBase):
    pass


class User(Base):
    __tablename__ = "user"
    id = Column(Integer, primary_key=True)
    name = Column(String)
    username = Column(String)
    email = Column(String)
    age = Column(Integer)
    role = Column(String)
    tier_id = Column(Integer, ForeignKey("tier.id"))
    department_id = Column(Integer, ForeignKey("department.id"))
    manager_id = Column(Integer, ForeignKey("user.id"))
    is_active = Column(Boolean, default=True)
    is_superuser = Column(Boolean, default=False)
    registration_date = Column(DateTime, default=func.now())
    archived = Column(Boolean, default=False)
    archived_at = Column(DateTime)
user/schemas.py
import datetime

from pydantic import BaseModel


class CreateUserSchema(BaseModel):
    name: str | None = None
    username: str | None = None
    email: str | None = None
    age: int | None = None
    role: str | None = None
    tier_id: int | None = None
    department_id: int | None = None
    manager_id: int | None = None
    is_active: bool | None = None
    is_superuser: bool | None = None


class ReadUserSchema(BaseModel):
    id: int
    name: str | None = None
    username: str | None = None
    email: str | None = None
    age: int | None = None
    role: str | None = None
    tier_id: int | None = None
    department_id: int | None = None
    manager_id: int | None = None
    is_active: bool
    is_superuser: bool
    registration_date: datetime.datetime
    archived: bool
    archived_at: datetime.datetime | None = None


class UpdateUserSchema(BaseModel):
    name: str | None = None
    username: str | None = None
    email: str | None = None
    age: int | None = None
    role: str | None = None
    tier_id: int | None = None
    department_id: int | None = None
    manager_id: int | None = None
    is_active: bool | None = None
    is_superuser: bool | None = None


class DeleteUserSchema(BaseModel):
    pass
story/model.py
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import DeclarativeBase


class Base(DeclarativeBase):
    pass


class Story(Base):
    __tablename__ = "story"
    id = Column(Integer, primary_key=True)
    name = Column(String)
story/schemas.py
from pydantic import BaseModel


class CreateStorySchema(BaseModel):
    name: str | None = None


class ReadStorySchema(BaseModel):
    id: int
    name: str | None = None


class UpdateStorySchema(BaseModel):
    name: str | None = None


class DeleteStorySchema(BaseModel):
    pass
task/model.py
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.orm import DeclarativeBase


class Base(DeclarativeBase):
    pass


class Task(Base):
    __tablename__ = "task"
    id = Column(Integer, primary_key=True)
    creator_id = Column(Integer, ForeignKey("user.id"))
    owner_id = Column(Integer, ForeignKey("user.id"))
    assigned_user_id = Column(Integer, ForeignKey("user.id"))
    story_id = Column(Integer, ForeignKey("story.id"))
    status = Column(String)
    priority = Column(String)
task/schemas.py
from pydantic import BaseModel


class CreateTaskSchema(BaseModel):
    creator_id: int | None = None
    owner_id: int | None = None
    assigned_user_id: int | None = None
    story_id: int | None = None
    status: str | None = None
    priority: str | None = None


class ReadTaskSchema(BaseModel):
    id: int
    creator_id: int | None = None
    owner_id: int | None = None
    assigned_user_id: int | None = None
    story_id: int | None = None
    status: str | None = None
    priority: str | None = None


class UpdateTaskSchema(BaseModel):
    creator_id: int | None = None
    owner_id: int | None = None
    assigned_user_id: int | None = None
    story_id: int | None = None
    status: str | None = None
    priority: str | None = None


class DeleteTaskSchema(BaseModel):
    pass

Basic Setup:

mymodel_router = crud_router(
    session=async_session,
    model=MyModel,
    create_schema=CreateMyModelSchema,
    update_schema=UpdateMyModelSchema,
    path="/mymodel",
    tags=["MyModel"],
)

With Custom Dependencies:

def get_current_user(token: str = Depends(oauth2_scheme)):
    # Implement user retrieval logic
    return ...

user_router = crud_router(
    session=async_session,
    model=User,
    create_schema=CreateUserSchema,
    update_schema=UpdateUserSchema,
    read_deps=[get_current_user],
    update_deps=[get_current_user],
    path="/users",
    tags=["Users"],
)

Adding Delete Endpoints:

product_router = crud_router(
    session=async_session,
    model=Product,
    create_schema=CreateProductSchema,
    update_schema=UpdateProductSchema,
    delete_schema=DeleteProductSchema,
    path="/products",
    tags=["Products"],
)

Customizing Path and Tags:

OrderCRUD = FastCRUD[
    Order,
    CreateOrderSchema,
    UpdateOrderSchema,
    UpdateOrderSchema,
    DeleteOrderSchema,
]
order_router = crud_router(
    session=async_session,
    model=Order,
    create_schema=CreateOrderSchema,
    update_schema=UpdateOrderSchema,
    crud=OrderCRUD(Order),
    path="/orders",
    tags=["Orders", "Sales"],
)

Integrating Multiple Models:

ProductCRUD = FastCRUD[
    Product,
    CreateProductSchema,
    UpdateProductSchema,
    UpdateProductSchema,
    DeleteProductSchema,
]
product_router = crud_router(
    session=async_session,
    model=Product,
    create_schema=CreateProductSchema,
    update_schema=UpdateProductSchema,
    crud=ProductCRUD(Product),
    path="/products",
    tags=["Inventory"],
)

CustomerCRUD = FastCRUD[
    Customer,
    CreateCustomerSchema,
    UpdateCustomerSchema,
    UpdateCustomerSchema,
    DeleteCustomerSchema,
]
customer_router = crud_router(
    session=async_session,
    model=Customer,
    create_schema=CreateCustomerSchema,
    update_schema=UpdateCustomerSchema,
    crud=CustomerCRUD(Customer),
    path="/customers",
    tags=["CRM"],
)

With Selective CRUD Methods:

MyModelCRUD = FastCRUD[
    MyModel,
    CreateMyModelSchema,
    UpdateMyModelSchema,
    UpdateMyModelSchema,
    DeleteMyModelSchema,
]
# Only include 'create' and 'read' methods
mymodel_router = crud_router(
    session=async_session,
    model=MyModel,
    create_schema=CreateMyModelSchema,
    update_schema=UpdateMyModelSchema,
    crud=MyModelCRUD(MyModel),
    path="/mymodel",
    tags=["MyModel"],
    included_methods=["create", "read"],
)

Using a Custom EndpointCreator:

class CustomEndpointCreator(EndpointCreator):
    def _custom_route(self):
        async def custom_endpoint():
            # Custom endpoint logic
            return {"message": "Custom route"}

        return custom_endpoint

        async def add_routes_to_router(self, ...):
            # First, add standard CRUD routes
            super().add_routes_to_router(...)

            # Now, add custom routes
            self.router.add_api_route(
                path="/custom",
                endpoint=self._custom_route(),
                methods=["GET"],
                tags=self.tags,
                # Other parameters as needed
            )

MyModelCRUD = FastCRUD[
    MyModel,
    CreateMyModelSchema,
    UpdateMyModelSchema,
    UpdateMyModelSchema,
    DeleteMyModelSchema,
]
mymodel_router = crud_router(
    session=async_session,
    model=MyModel,
    create_schema=CreateMyModelSchema,
    update_schema=UpdateMyModelSchema,
    crud=MyModelCRUD(MyModel),
    path="/mymodel",
    tags=["MyModel"],
    endpoint_creator=CustomEndpointCreator,
)

app.include_router(mymodel_router)

Customizing Endpoint Names:

task_router = crud_router(
    session=async_session,
    model=Task,
    create_schema=CreateTaskSchema,
    update_schema=UpdateTaskSchema,
    path="/tasks",
    tags=["Task Management"],
    endpoint_names={
        "create": "add_task",
        "read": "get_task",
        "update": "modify_task",
        "delete": "remove_task",
        "db_delete": "permanently_remove_task",
        "read_multi": "list_tasks",
    },
)

Using FilterConfig with dict:

from fastapi import FastAPI
from fastcrud import crud_router

from .database import async_session
from .mymodel.model import MyModel
from .mymodel.schemas import CreateMyModelSchema, UpdateMyModelSchema

app = FastAPI()

mymodel_router = crud_router(
    session=async_session,
    model=MyModel,
    create_schema=CreateMyModelSchema,
    update_schema=UpdateMyModelSchema,
    filter_config=FilterConfig(filters={"id": None, "name": "default"}),
)
# Adds CRUD routes with filtering capabilities
app.include_router(mymodel_router, prefix="/mymodel")

# Explanation:
# The FilterConfig specifies that 'id' should be a query parameter with no default value
# and 'name' should be a query parameter with a default value of 'default'.
# When fetching multiple items, you can filter by these parameters.
# Example GET request: /mymodel/get_multi?id=1&name=example

Using FilterConfig with keyword arguments:

from fastapi import FastAPI
from fastcrud import crud_router

from .database import async_session
from .mymodel.model import MyModel
from .mymodel.schemas import CreateMyModelSchema, UpdateMyModelSchema

app = FastAPI()

mymodel_router = crud_router(
    session=async_session,
    model=MyModel,
    create_schema=CreateMyModelSchema,
    update_schema=UpdateMyModelSchema,
    filter_config=FilterConfig(id=None, name="default"),
)
# Adds CRUD routes with filtering capabilities
app.include_router(mymodel_router, prefix="/mymodel")

# Explanation:
# The FilterConfig specifies that 'id' should be a query parameter with no default value
# and 'name' should be a query parameter with a default value of 'default'.
# When fetching multiple items, you can filter by these parameters.
# Example GET request: /mymodel/get_multi?id=1&name=example
Source code in fastcrud/endpoint/crud_router.py
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
def crud_router(
    session: Callable,
    model: ModelType,
    create_schema: Type[CreateSchemaType],
    update_schema: Type[UpdateSchemaType],
    crud: Optional[FastCRUD] = None,
    delete_schema: Optional[Type[DeleteSchemaType]] = None,
    path: str = "",
    tags: Optional[list[Union[str, Enum]]] = None,
    include_in_schema: bool = True,
    create_deps: Sequence[Callable] = [],
    read_deps: Sequence[Callable] = [],
    read_multi_deps: Sequence[Callable] = [],
    update_deps: Sequence[Callable] = [],
    delete_deps: Sequence[Callable] = [],
    db_delete_deps: Sequence[Callable] = [],
    included_methods: Optional[list[str]] = None,
    deleted_methods: Optional[list[str]] = None,
    endpoint_creator: Optional[Type[EndpointCreator]] = None,
    is_deleted_column: str = "is_deleted",
    deleted_at_column: str = "deleted_at",
    updated_at_column: str = "updated_at",
    endpoint_names: Optional[dict[str, str]] = None,
    filter_config: Optional[Union[FilterConfig, dict]] = None,
) -> APIRouter:
    """
    Creates and configures a FastAPI router with CRUD endpoints for a given model.

    This utility function streamlines the process of setting up a router for CRUD operations,
    using a custom `EndpointCreator` if provided, and managing dependency injections as well
    as selective method inclusions or exclusions.

    Args:
        session: The SQLAlchemy async session.
        model: The SQLAlchemy model.
        create_schema: Pydantic schema for creating an item.
        update_schema: Pydantic schema for updating an item.
        crud: An optional `FastCRUD` instance. If not provided, uses `FastCRUD(model)`.
        delete_schema: Optional Pydantic schema for deleting an item.
        path: Base path for the CRUD endpoints.
        tags: Optional list of tags for grouping endpoints in the documentation.
        include_in_schema: Whether to include the created endpoints in the OpenAPI schema.
        create_deps: Optional list of functions to be injected as dependencies for the create endpoint.
        read_deps: Optional list of functions to be injected as dependencies for the read endpoint.
        read_multi_deps: Optional list of functions to be injected as dependencies for the read multiple items endpoint.
        update_deps: Optional list of functions to be injected as dependencies for the update endpoint.
        delete_deps: Optional list of functions to be injected as dependencies for the delete endpoint.
        db_delete_deps: Optional list of functions to be injected as dependencies for the hard delete endpoint.
        included_methods: Optional list of CRUD methods to include. If `None`, all methods are included.
        deleted_methods: Optional list of CRUD methods to exclude.
        endpoint_creator: Optional custom class derived from `EndpointCreator` for advanced customization.
        is_deleted_column: Optional column name to use for indicating a soft delete. Defaults to `"is_deleted"`.
        deleted_at_column: Optional column name to use for storing the timestamp of a soft delete. Defaults to `"deleted_at"`.
        updated_at_column: Optional column name to use for storing the timestamp of an update. Defaults to `"updated_at"`.
        endpoint_names: Optional dictionary to customize endpoint names for CRUD operations. Keys are operation types
                        (`"create"`, `"read"`, `"update"`, `"delete"`, `"db_delete"`, `"read_multi"`), and
                        values are the custom names to use. Unspecified operations will use default names.
        filter_config: Optional `FilterConfig` instance or dictionary to configure filters for the `read_multi` endpoint.

    Returns:
        Configured `APIRouter` instance with the CRUD endpoints.

    Raises:
        ValueError: If both `included_methods` and `deleted_methods` are provided.

    Examples:
        ??? example "Models and Schemas Used Below"

            ??? example "`mymodel/model.py`"

                ```python
                --8<--
                fastcrud/examples/mymodel/model.py:imports
                fastcrud/examples/mymodel/model.py:model_simple
                --8<--
                ```

            ??? example "`mymodel/schemas.py`"

                ```python
                --8<--
                fastcrud/examples/mymodel/schemas.py:imports
                fastcrud/examples/mymodel/schemas.py:createschema
                fastcrud/examples/mymodel/schemas.py:updateschema
                fastcrud/examples/mymodel/schemas.py:deleteschema
                --8<--
                ```

            ---

            ??? example "`customer/model.py`"

                ```python
                --8<--
                fastcrud/examples/customer/model.py:imports
                fastcrud/examples/customer/model.py:model
                --8<--
                ```

            ??? example "`customer/schemas.py`"

                ```python
                --8<--
                fastcrud/examples/customer/schemas.py:imports
                fastcrud/examples/customer/schemas.py:createschema
                fastcrud/examples/customer/schemas.py:readschema
                fastcrud/examples/customer/schemas.py:updateschema
                fastcrud/examples/customer/schemas.py:deleteschema
                --8<--
                ```

            ??? example "`product/model.py`"

                ```python
                --8<--
                fastcrud/examples/product/model.py:imports
                fastcrud/examples/product/model.py:model
                --8<--
                ```

            ??? example "`product/schemas.py`"

                ```python
                --8<--
                fastcrud/examples/product/schemas.py:imports
                fastcrud/examples/product/schemas.py:createschema
                fastcrud/examples/product/schemas.py:readschema
                fastcrud/examples/product/schemas.py:updateschema
                fastcrud/examples/product/schemas.py:deleteschema
                --8<--
                ```

            ??? example "`order/model.py`"

                ```python
                --8<--
                fastcrud/examples/order/model.py:imports
                fastcrud/examples/order/model.py:model
                --8<--
                ```

            ??? example "`order/schemas.py`"

                ```python
                --8<--
                fastcrud/examples/order/schemas.py:imports
                fastcrud/examples/order/schemas.py:createschema
                fastcrud/examples/order/schemas.py:readschema
                fastcrud/examples/order/schemas.py:updateschema
                fastcrud/examples/order/schemas.py:deleteschema
                --8<--
                ```

            ---

            ??? example "`tier/model.py`"

                ```python
                --8<--
                fastcrud/examples/tier/model.py:imports
                fastcrud/examples/tier/model.py:model
                --8<--
                ```

            ??? example "`tier/schemas.py`"

                ```python
                --8<--
                fastcrud/examples/tier/schemas.py:imports
                fastcrud/examples/tier/schemas.py:readschema
                --8<--
                ```

            ??? example "`department/model.py`"

                ```python
                --8<--
                fastcrud/examples/department/model.py:imports
                fastcrud/examples/department/model.py:model
                --8<--
                ```

            ??? example "`department/schemas.py`"

                ```python
                --8<--
                fastcrud/examples/department/schemas.py:imports
                fastcrud/examples/department/schemas.py:readschema
                --8<--
                ```

            ??? example "`user/model.py`"

                ```python
                --8<--
                fastcrud/examples/user/model.py:imports
                fastcrud/examples/user/model.py:model
                --8<--
                ```

            ??? example "`user/schemas.py`"

                ```python
                --8<--
                fastcrud/examples/user/schemas.py:imports
                fastcrud/examples/user/schemas.py:createschema
                fastcrud/examples/user/schemas.py:readschema
                fastcrud/examples/user/schemas.py:updateschema
                fastcrud/examples/user/schemas.py:deleteschema
                --8<--
                ```

            ??? example "`story/model.py`"

                ```python
                --8<--
                fastcrud/examples/story/model.py:imports
                fastcrud/examples/story/model.py:model
                --8<--
                ```

            ??? example "`story/schemas.py`"

                ```python
                --8<--
                fastcrud/examples/story/schemas.py:imports
                fastcrud/examples/story/schemas.py:createschema
                fastcrud/examples/story/schemas.py:readschema
                fastcrud/examples/story/schemas.py:updateschema
                fastcrud/examples/story/schemas.py:deleteschema
                --8<--
                ```

            ??? example "`task/model.py`"

                ```python
                --8<--
                fastcrud/examples/task/model.py:imports
                fastcrud/examples/task/model.py:model
                --8<--
                ```

            ??? example "`task/schemas.py`"

                ```python
                --8<--
                fastcrud/examples/task/schemas.py:imports
                fastcrud/examples/task/schemas.py:createschema
                fastcrud/examples/task/schemas.py:readschema
                fastcrud/examples/task/schemas.py:updateschema
                fastcrud/examples/task/schemas.py:deleteschema
                --8<--
                ```

        Basic Setup:

        ```python
        mymodel_router = crud_router(
            session=async_session,
            model=MyModel,
            create_schema=CreateMyModelSchema,
            update_schema=UpdateMyModelSchema,
            path="/mymodel",
            tags=["MyModel"],
        )
        ```

        With Custom Dependencies:

        ```python
        def get_current_user(token: str = Depends(oauth2_scheme)):
            # Implement user retrieval logic
            return ...

        user_router = crud_router(
            session=async_session,
            model=User,
            create_schema=CreateUserSchema,
            update_schema=UpdateUserSchema,
            read_deps=[get_current_user],
            update_deps=[get_current_user],
            path="/users",
            tags=["Users"],
        )
        ```

        Adding Delete Endpoints:
        ```python
        product_router = crud_router(
            session=async_session,
            model=Product,
            create_schema=CreateProductSchema,
            update_schema=UpdateProductSchema,
            delete_schema=DeleteProductSchema,
            path="/products",
            tags=["Products"],
        )
        ```

        Customizing Path and Tags:
        ```python
        OrderCRUD = FastCRUD[
            Order,
            CreateOrderSchema,
            UpdateOrderSchema,
            UpdateOrderSchema,
            DeleteOrderSchema,
        ]
        order_router = crud_router(
            session=async_session,
            model=Order,
            create_schema=CreateOrderSchema,
            update_schema=UpdateOrderSchema,
            crud=OrderCRUD(Order),
            path="/orders",
            tags=["Orders", "Sales"],
        )
        ```

        Integrating Multiple Models:
        ```python
        ProductCRUD = FastCRUD[
            Product,
            CreateProductSchema,
            UpdateProductSchema,
            UpdateProductSchema,
            DeleteProductSchema,
        ]
        product_router = crud_router(
            session=async_session,
            model=Product,
            create_schema=CreateProductSchema,
            update_schema=UpdateProductSchema,
            crud=ProductCRUD(Product),
            path="/products",
            tags=["Inventory"],
        )

        CustomerCRUD = FastCRUD[
            Customer,
            CreateCustomerSchema,
            UpdateCustomerSchema,
            UpdateCustomerSchema,
            DeleteCustomerSchema,
        ]
        customer_router = crud_router(
            session=async_session,
            model=Customer,
            create_schema=CreateCustomerSchema,
            update_schema=UpdateCustomerSchema,
            crud=CustomerCRUD(Customer),
            path="/customers",
            tags=["CRM"],
        )
        ```

        With Selective CRUD Methods:

        ```python
        MyModelCRUD = FastCRUD[
            MyModel,
            CreateMyModelSchema,
            UpdateMyModelSchema,
            UpdateMyModelSchema,
            DeleteMyModelSchema,
        ]
        # Only include 'create' and 'read' methods
        mymodel_router = crud_router(
            session=async_session,
            model=MyModel,
            create_schema=CreateMyModelSchema,
            update_schema=UpdateMyModelSchema,
            crud=MyModelCRUD(MyModel),
            path="/mymodel",
            tags=["MyModel"],
            included_methods=["create", "read"],
        )
        ```

        Using a Custom `EndpointCreator`:

        ```python
        class CustomEndpointCreator(EndpointCreator):
            def _custom_route(self):
                async def custom_endpoint():
                    # Custom endpoint logic
                    return {"message": "Custom route"}

                return custom_endpoint

                async def add_routes_to_router(self, ...):
                    # First, add standard CRUD routes
                    super().add_routes_to_router(...)

                    # Now, add custom routes
                    self.router.add_api_route(
                        path="/custom",
                        endpoint=self._custom_route(),
                        methods=["GET"],
                        tags=self.tags,
                        # Other parameters as needed
                    )

        MyModelCRUD = FastCRUD[
            MyModel,
            CreateMyModelSchema,
            UpdateMyModelSchema,
            UpdateMyModelSchema,
            DeleteMyModelSchema,
        ]
        mymodel_router = crud_router(
            session=async_session,
            model=MyModel,
            create_schema=CreateMyModelSchema,
            update_schema=UpdateMyModelSchema,
            crud=MyModelCRUD(MyModel),
            path="/mymodel",
            tags=["MyModel"],
            endpoint_creator=CustomEndpointCreator,
        )

        app.include_router(mymodel_router)
        ```

        Customizing Endpoint Names:

        ```python
        task_router = crud_router(
            session=async_session,
            model=Task,
            create_schema=CreateTaskSchema,
            update_schema=UpdateTaskSchema,
            path="/tasks",
            tags=["Task Management"],
            endpoint_names={
                "create": "add_task",
                "read": "get_task",
                "update": "modify_task",
                "delete": "remove_task",
                "db_delete": "permanently_remove_task",
                "read_multi": "list_tasks",
            },
        )
        ```

        Using `FilterConfig` with `dict`:

        ```python
        from fastapi import FastAPI
        from fastcrud import crud_router

        from .database import async_session
        from .mymodel.model import MyModel
        from .mymodel.schemas import CreateMyModelSchema, UpdateMyModelSchema

        app = FastAPI()

        mymodel_router = crud_router(
            session=async_session,
            model=MyModel,
            create_schema=CreateMyModelSchema,
            update_schema=UpdateMyModelSchema,
            filter_config=FilterConfig(filters={"id": None, "name": "default"}),
        )
        # Adds CRUD routes with filtering capabilities
        app.include_router(mymodel_router, prefix="/mymodel")

        # Explanation:
        # The FilterConfig specifies that 'id' should be a query parameter with no default value
        # and 'name' should be a query parameter with a default value of 'default'.
        # When fetching multiple items, you can filter by these parameters.
        # Example GET request: /mymodel/get_multi?id=1&name=example
        ```

        Using `FilterConfig` with keyword arguments:

        ```python
        from fastapi import FastAPI
        from fastcrud import crud_router

        from .database import async_session
        from .mymodel.model import MyModel
        from .mymodel.schemas import CreateMyModelSchema, UpdateMyModelSchema

        app = FastAPI()

        mymodel_router = crud_router(
            session=async_session,
            model=MyModel,
            create_schema=CreateMyModelSchema,
            update_schema=UpdateMyModelSchema,
            filter_config=FilterConfig(id=None, name="default"),
        )
        # Adds CRUD routes with filtering capabilities
        app.include_router(mymodel_router, prefix="/mymodel")

        # Explanation:
        # The FilterConfig specifies that 'id' should be a query parameter with no default value
        # and 'name' should be a query parameter with a default value of 'default'.
        # When fetching multiple items, you can filter by these parameters.
        # Example GET request: /mymodel/get_multi?id=1&name=example
        ```
    """

    crud = crud or FastCRUD(
        model=model,
        is_deleted_column=is_deleted_column,
        deleted_at_column=deleted_at_column,
        updated_at_column=updated_at_column,
    )

    endpoint_creator_class = endpoint_creator or EndpointCreator
    endpoint_creator_instance = endpoint_creator_class(
        session=session,
        model=model,
        crud=crud,
        create_schema=create_schema,  # type: ignore
        update_schema=update_schema,  # type: ignore
        include_in_schema=include_in_schema,
        delete_schema=delete_schema,  # type: ignore
        path=path,
        tags=tags,
        is_deleted_column=is_deleted_column,
        deleted_at_column=deleted_at_column,
        updated_at_column=updated_at_column,
        endpoint_names=endpoint_names,
        filter_config=filter_config,
    )

    endpoint_creator_instance.add_routes_to_router(
        create_deps=create_deps,
        read_deps=read_deps,
        read_multi_deps=read_multi_deps,
        update_deps=update_deps,
        delete_deps=delete_deps,
        db_delete_deps=db_delete_deps,
        included_methods=included_methods,
        deleted_methods=deleted_methods,
    )

    return endpoint_creator_instance.router