# fastapi-admin

### 项目默认会安装

- fastapi_pagination
- fastapi
- tortoise-orm

### 目前只支持pydantic-v2

## 功能示例

> main.py

```python
from typing import Callable

from fastapi import FastAPI, Depends, HTTPException, Request
from fastapi_pagination import Params
from fastapi_pagination.ext.tortoise import paginate
from tortoise.contrib.fastapi import register_tortoise
from uvicorn import run
from fastapi_tortoise_crud import ModelCrud, BaseApiOut
from models import User


def aa(request: Request, test_params: str = '333'):
    # raise HTTPException(status_code=400, detail='aaa')
    print('aaa')
    return {'id': 1, 'username': 'aaa'}


def bb():
    return 'bb'


class UserCrud(ModelCrud):
    def route_list(self) -> Callable:
        schema_filters = self.schema_filters

        async def route(filters: schema_filters, params: Params = Depends(), order_by: str = '-create_time',
                        a=Depends(aa)):
            print(a)
            filter_item = await self.pre_list(filters)
            queryset = self.model.filter(**filter_item)
            if order_by:
                queryset = queryset.order_by(*order_by.split(','))
            data = await paginate(queryset, params, True)
            return BaseApiOut(data=data)

        return route


user = UserCrud(User,
                schema_filters=User.schema_filters(include=('username', 'status')),
                schema_read=User.schema_read(exclude=('password', 'id')),
                schema_list=User.schema_list(),
                depends_create=False,
                dependencies=[Depends(bb)],
                depends_read=[Depends(aa)]
                )
app = FastAPI()

app.include_router(user, prefix='/user', tags=['用户管理'])


@app.get("/")
async def root():
    return {"message": "Hello World"}


@user.get('/bba')
async def bba():
    return 'bba'


pass

register_tortoise(
    app,
    db_url="sqlite://data.db",
    modules={"models": ["models"]},
    generate_schemas=True,
    add_exception_handlers=True,
)

if __name__ == '__main__':
    run("main:app", port=2103, reload=True)
    pass

```

> models.py

```python
from fastapi_tortoise_crud.model import BaseModel
from tortoise import fields


class User(BaseModel):
    username = fields.CharField(64)
    password = fields.CharField(32)
    nickname = fields.CharField(32, null=True, default='游客')

```

### 路由
![img.png](img.png)
