更新readme:数据迁移和生成CRUD,路由配置

This commit is contained in:
zhusr39924 2024-07-03 10:41:54 +08:00
parent 5af56e956c
commit afe3b042d9

View File

@ -18,11 +18,11 @@ Typer 官方文档https://typer.tiangolo.com/
SQLAlchemy 2.0 (官方): https://docs.sqlalchemy.org/en/20/intro.html#installation SQLAlchemy 2.0 (官方): https://docs.sqlalchemy.org/en/20/intro.html#installation
SQLAlchemy 1.4 迁移到 2.0 SQLAlchemy 1.4 迁移到 2.0 官方https://docs.sqlalchemy.org/en/20/changelog/whatsnew_20.html#whatsnew-20-orm-declarative-typing
官方https://docs.sqlalchemy.org/en/20/changelog/whatsnew_20.html#whatsnew-20-orm-declarative-typing
PEP 484 语法官方https://peps.python.org/pep-0484/ PEP 484 语法官方https://peps.python.org/pep-0484/
## 项目结构 ## 项目结构
使用的是仿照 Django 项目结构: 使用的是仿照 Django 项目结构:
@ -133,7 +133,6 @@ http://127.0.0.1:9000/docs
``` ```
Git更新ignore文件直接修改gitignore是不会生效的需要先去掉已经托管的文件修改完成之后再重新添加并提交。 Git更新ignore文件直接修改gitignore是不会生效的需要先去掉已经托管的文件修改完成之后再重新添加并提交。
``` ```
第一步: 第一步:
git rm -r --cached . git rm -r --cached .
@ -148,9 +147,7 @@ git commit -m "clear cached"
``` ```
## 新的数据迁移 ## 新的数据迁移
执行数据库迁移命令(终端执行) 执行数据库迁移命令(终端执行)
- 新建模型: - 新建模型:
- 在你的app目录下新建一个models目录__init__.py导入你需要迁移的models - 在你的app目录下新建一个models目录__init__.py导入你需要迁移的models
```python ```python
@ -242,36 +239,36 @@ urlpatterns = [
# 日期查询 # 日期查询
# 值的类型str # 值的类型str
# 值得格式:%Y-%m-%d2023-05-14 # 值得格式:%Y-%m-%d2023-05-14
字段名称 = ("date", 值) 字段名称=("date", 值)
# 模糊查询 # 模糊查询
# 值的类型: str # 值的类型: str
字段名称 = ("like", 值) 字段名称=("like", 值)
# in 查询 # in 查询
# 值的类型list # 值的类型list
字段名称 = ("in", 值) 字段名称=("in", 值)
# 时间区间查询 # 时间区间查询
# 值的类型tuple 或者 list # 值的类型tuple 或者 list
字段名称 = ("between", 值) 字段名称=("between", 值)
# 月份查询 # 月份查询
# 值的类型str # 值的类型str
# 值的格式:%Y-%m2023-05 # 值的格式:%Y-%m2023-05
字段名称 = ("month", 值) 字段名称=("month", 值)
# 不等于查询 # 不等于查询
字段名称 = ("!=", 值) 字段名称=("!=", 值)
# 大于查询 # 大于查询
字段名称 = (">", 值) 字段名称=(">", 值)
# 等于 None # 等于 None
字段名称 = ("None") 字段名称=("None")
# 不等于 None # 不等于 None
字段名称 = ("not None") 字段名称=("not None")
``` ```
代码部分: 代码部分:
@ -325,15 +322,15 @@ def __dict_filter(self, **kwargs) -> list[BinaryExpression]:
查询所有用户id为1或2或 4或6并且email不为空并且名称包括李 查询所有用户id为1或2或 4或6并且email不为空并且名称包括李
```python ```python
users = UserDal(db).get_datas(limit=0, id=("in", [1, 2, 4, 6]), email=("not None",), name=("like", "李")) users = UserDal(db).get_datas(limit=0, id=("in", [1,2,4,6]), email=("not None", ), name=("like", "李"))
# limit=0表示返回所有结果数据 # limit=0表示返回所有结果数据
# 这里的 get_datas 默认返回的是 pydantic 模型数据 # 这里的 get_datas 默认返回的是 pydantic 模型数据
# 如果需要返回用户对象列表,使用如下语句: # 如果需要返回用户对象列表,使用如下语句:
users = UserDal(db).get_datas( users = UserDal(db).get_datas(
limit=0, limit=0,
id=("in", [1, 2, 4, 6]), id=("in", [1,2,4,6]),
email=("not None",), email=("not None", ),
name=("like", "李"), name=("like", "李"),
v_return_objs=True v_return_objs=True
) )
@ -344,7 +341,7 @@ users = UserDal(db).get_datas(
查询第一页,每页两条数据,并返回总数,同样可以通过 `get_datas` 实现原始查询方式: 查询第一页,每页两条数据,并返回总数,同样可以通过 `get_datas` 实现原始查询方式:
```python ```python
v_where = [VadminUser.id.in_([1, 2, 4, 6]), VadminUser.email.isnot(None), VadminUser.name.like(f"%李%")] v_where = [VadminUser.id.in_([1,2,4,6]), VadminUser.email.isnot(None), VadminUser.name.like(f"%李%")]
users, count = UserDal(db).get_datas(limit=2, v_where=v_where, v_return_count=True) users, count = UserDal(db).get_datas(limit=2, v_where=v_where, v_return_count=True)
# 这里的 get_datas 默认返回的是 pydantic 模型数据 # 这里的 get_datas 默认返回的是 pydantic 模型数据
@ -353,7 +350,7 @@ users, count = UserDal(db).get_datas(
limit=2, limit=2,
v_where=v_where, v_where=v_where,
v_return_count=True v_return_count=True
v_return_objs = True v_return_objs=True
) )
``` ```