更新readme
This commit is contained in:
parent
9acac1711e
commit
ecf90fec31
72
README.md
72
README.md
@ -189,62 +189,52 @@ class AccountWechats(AccountWechatsBase):
|
||||
from_attributes = True # 允许与ORM对象进行直接转换
|
||||
```
|
||||
|
||||
### 3.创建 CRUD (可选,不知道创建什么逻辑可以先跳过):
|
||||
### 3.创建 CRUD:
|
||||
```python
|
||||
# filepath: ExamService/mooc/crud/crud_account.py
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import Optional
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from mooc.crud.crud_base import CRUDBase
|
||||
from mooc.models.account import AccountWechats
|
||||
from mooc.schemas.account import AccountWechatsCreate, AccountWechatsUpdate
|
||||
|
||||
class CRUDAccountWechats:
|
||||
class CRUDAccountWechats(CRUDBase[AccountWechats, AccountWechatsCreate, AccountWechatsUpdate]):
|
||||
"""
|
||||
创建CRUD
|
||||
负责对AccountWechats模型进行增删改查操作的逻辑封装。
|
||||
AccountWechats的CRUD操作类
|
||||
继承自CRUDBase基类,实现基础的增删改查功能
|
||||
可以根据需要添加自定义的查询方法
|
||||
"""
|
||||
def get_by_uniacid(self, db: Session, *, uniacid: int) -> Optional[AccountWechats]:
|
||||
"""
|
||||
根据uniacid查询微信公众号账号
|
||||
:param db: 数据库会话
|
||||
:param uniacid: 统一公众号ID
|
||||
:return: AccountWechats对象或None
|
||||
"""
|
||||
return db.query(self.model).filter(self.model.uniacid == uniacid).first()
|
||||
|
||||
def create(self, db: Session, obj_in: AccountWechatsCreate) -> AccountWechats:
|
||||
"""
|
||||
创建记录:将Pydantic的输入数据转换为数据库模型实例并保存。
|
||||
"""
|
||||
db_obj = AccountWechats(**obj_in.dict()) # 将表单数据解包到模型实例
|
||||
db.add(db_obj) # 添加到会话
|
||||
db.commit() # 提交事务
|
||||
db.refresh(db_obj) # 刷新实例,获取数据库中的最新状态
|
||||
return db_obj
|
||||
# 实例化CRUD对象,方便在其他地方直接导入使用
|
||||
account_wechats = CRUDAccountWechats(AccountWechats)
|
||||
|
||||
def get(self, db: Session, acid: int) -> Optional[AccountWechats]:
|
||||
# 使用示例:
|
||||
"""
|
||||
根据主键 acid 查询单条数据。
|
||||
"""
|
||||
return db.query(AccountWechats).filter(AccountWechats.acid == acid).first()
|
||||
# 创建新记录
|
||||
new_account = account_wechats.create(db, obj_in=account_create_schema)
|
||||
|
||||
def update(
|
||||
self, db: Session, *, db_obj: AccountWechats, obj_in: AccountWechatsUpdate
|
||||
) -> AccountWechats:
|
||||
"""
|
||||
更新记录:只修改传递进来的字段,未设置的字段不动。
|
||||
"""
|
||||
for field, value in obj_in.dict(exclude_unset=True).items():
|
||||
setattr(db_obj, field, value) # 更新模型实例属性
|
||||
db.add(db_obj)
|
||||
db.commit()
|
||||
db.refresh(db_obj)
|
||||
return db_obj
|
||||
# 根据ID获取记录
|
||||
account = account_wechats.get(db, id=1)
|
||||
|
||||
def delete(self, db: Session, acid: int) -> None:
|
||||
"""
|
||||
删除记录:物理删除或逻辑删除都可在此处实现。
|
||||
"""
|
||||
obj = db.query(AccountWechats).filter(AccountWechats.acid == acid).first()
|
||||
if obj:
|
||||
db.delete(obj)
|
||||
db.commit()
|
||||
# 根据uniacid获取记录
|
||||
account = account_wechats.get_by_uniacid(db, uniacid=100)
|
||||
|
||||
# 实例化CRUD对象,方便在业务代码中直接引用
|
||||
account_wechats = CRUDAccountWechats()
|
||||
# 更新记录
|
||||
updated = account_wechats.update(db, db_obj=existing_account, obj_in=update_data)
|
||||
|
||||
# 删除记录
|
||||
account_wechats.remove(db, id=1)
|
||||
"""
|
||||
```
|
||||
|
||||
### 4.注册模型:
|
||||
```python
|
||||
# filepath: ExamService/mooc/db/base.py
|
||||
|
Loading…
Reference in New Issue
Block a user