更新readme:数据迁移和生成CRUD,路由配置
This commit is contained in:
parent
39194d51c0
commit
5af56e956c
@ -18,11 +18,11 @@ Typer 官方文档:https://typer.tiangolo.com/
|
||||
|
||||
SQLAlchemy 2.0 (官方): https://docs.sqlalchemy.org/en/20/intro.html#installation
|
||||
|
||||
SQLAlchemy 1.4 迁移到 2.0 (官方):https://docs.sqlalchemy.org/en/20/changelog/whatsnew_20.html#whatsnew-20-orm-declarative-typing
|
||||
SQLAlchemy 1.4 迁移到 2.0
|
||||
(官方):https://docs.sqlalchemy.org/en/20/changelog/whatsnew_20.html#whatsnew-20-orm-declarative-typing
|
||||
|
||||
PEP 484 语法(官方):https://peps.python.org/pep-0484/
|
||||
|
||||
|
||||
## 项目结构
|
||||
|
||||
使用的是仿照 Django 项目结构:
|
||||
@ -133,6 +133,7 @@ http://127.0.0.1:9000/docs
|
||||
```
|
||||
|
||||
Git更新ignore文件直接修改gitignore是不会生效的,需要先去掉已经托管的文件,修改完成之后再重新添加并提交。
|
||||
|
||||
```
|
||||
第一步:
|
||||
git rm -r --cached .
|
||||
@ -146,8 +147,45 @@ git add .
|
||||
git commit -m "clear cached"
|
||||
```
|
||||
|
||||
## 新的数据迁移
|
||||
|
||||
执行数据库迁移命令(终端执行)
|
||||
|
||||
- 新建模型:
|
||||
- 在你的app目录下新建一个models目录,__init__.py导入你需要迁移的models
|
||||
```python
|
||||
# app/.../your_app/models/__init__.py
|
||||
from .your_model import YourModel,YourModel2
|
||||
|
||||
```
|
||||
```python
|
||||
# app/.../your_app/models/your_model.py
|
||||
from db.db_base import BaseModel
|
||||
|
||||
class YourModel(BaseModel):
|
||||
# 定义你的model
|
||||
...
|
||||
|
||||
class YourModel2(BaseModel):
|
||||
# 定义你的model
|
||||
...
|
||||
```
|
||||
- 根据模型配置你的alembic:
|
||||
```
|
||||
# alembic.ini
|
||||
[dev]
|
||||
...
|
||||
sqlalchemy.url = mysql+pymysql://your_username:password@ip:port/kinit
|
||||
...
|
||||
```
|
||||
```python
|
||||
# alembic/env.py
|
||||
# 导入项目中的基本映射类,与 需要迁移的 ORM 模型
|
||||
from apps.vadmin.auth.models import *
|
||||
...
|
||||
from apps.xxx.your_app.models import *
|
||||
```
|
||||
- 执行脚本:
|
||||
```shell
|
||||
# 执行命令(生产环境):
|
||||
python main.py migrate
|
||||
@ -162,6 +200,40 @@ alembic --name dev upgrade head
|
||||
|
||||
生成迁移文件后,会在alembic迁移目录中的version目录中多个迁移文件
|
||||
|
||||
## 新的CRUD
|
||||
|
||||
- 新的模型文件已经建好(上一步迁移时必须)
|
||||
- 在 scripts/crud_generate/main.py 添加执行命令
|
||||
|
||||
```python
|
||||
# scripts/crud_generate/main.py
|
||||
if __name__ == '__main__':
|
||||
from apps.xxx.your_app.models import YourModel
|
||||
|
||||
crud = CrudGenerate(YourModel, "中文名", "en_name")
|
||||
# 只打印代码,不执行创建写入
|
||||
crud.generate_codes()
|
||||
# 创建并写入代码
|
||||
crud.main()
|
||||
```
|
||||
|
||||
- 生成后会自动创建crud, params,schema, views
|
||||
|
||||
## 新的路由配置
|
||||
|
||||
```python
|
||||
# application/urls.py
|
||||
|
||||
from apps.xxx.your_app.views import app as your_app
|
||||
|
||||
urlpatterns = [
|
||||
...,
|
||||
{"ApiRouter": your_app, "prefix": "/your_router", "tags": ["your_tag"]},
|
||||
]
|
||||
```
|
||||
|
||||
完成后在 http://127.0.0.1:9000/docs 验证生成的接口
|
||||
|
||||
## 查询数据
|
||||
|
||||
### 自定义的一些查询过滤
|
||||
|
Loading…
x
Reference in New Issue
Block a user