diff --git a/README.md b/README.md index ee86805..3aff25f 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,11 @@ # RG-FastAPI-SQLModel 模板项目 一个基于FastAPI和SQLModel的现代化Python Web应用模板,提供了快速开发RESTful API的基础架构。 - ## 项目特点 - 基于FastAPI构建的高性能异步Web框架 - 使用SQLModel进行数据库ORM操作,结合了SQLAlchemy和Pydantic的优点 -- 完整的项目结构,包括路由、服务层、实体模型和DTO +- 完整的项目结构,包括路由(自动注册)、服务层、实体模型和DTO - 内置异常处理机制 - 中间件支持,包括数据库会话管理 - 实用工具集合,包括日志、文件操作、IP工具等 diff --git a/entity/base_entity.py b/entity/base_entity.py index 159ab6a..0d796ae 100644 --- a/entity/base_entity.py +++ b/entity/base_entity.py @@ -1,7 +1,7 @@ from sqlalchemy import Column, BigInteger, Select, Delete, Update from common.global_enums import IsDelete -from utils import current_timestamp +from utils import current_timestamp, get_uuid # 上下文变量,控制是否启用逻辑删除过滤 _SOFT_DELETE_ENABLED = True @@ -9,7 +9,7 @@ from sqlmodel import SQLModel, Field class DbBaseModel(SQLModel, table=False): - id: str = Field(default=None, max_length=32, primary_key=True) + id: str = Field(default_factory=get_uuid, max_length=32, primary_key=True) created_time: int = Field( default_factory=current_timestamp, sa_type=BigInteger, diff --git a/entity/dto/UserDto.py b/entity/dto/user_dto.py similarity index 100% rename from entity/dto/UserDto.py rename to entity/dto/user_dto.py diff --git a/router/user_app.py b/router/user_app.py index 3d234d5..c844fc7 100644 --- a/router/user_app.py +++ b/router/user_app.py @@ -1,6 +1,6 @@ from fastapi import APIRouter, Query -from entity.dto.UserDto import UserQueryPageReq, UserQueryReq +from entity.dto.user_dto import UserQueryPageReq, UserQueryReq from router import BaseController, unified_resp from service.user_service import UserService diff --git a/service/base_service.py b/service/base_service.py index 31e54d2..c35cb9b 100644 --- a/service/base_service.py +++ b/service/base_service.py @@ -172,7 +172,7 @@ class BaseService(Generic[T]): update_stmt = cls.model.update().where(cls.model.id == pid).values(**data) session = cls.get_db() result = await session.execute(update_stmt) - return result.rowcount() + return result.rowcount @classmethod async def update_many_by_id(cls, data_list)->None: @@ -183,7 +183,7 @@ class BaseService(Generic[T]): @classmethod async def get_by_id(cls, pid)->T: - stmt = cls.model.select(cls.model.id == pid) + stmt = cls.model.select().where(cls.model.id == pid) session = cls.get_db() return await session.scalar(stmt) @@ -203,14 +203,14 @@ class BaseService(Generic[T]): del_stmt = cls.model.delete().where(cls.model.id == pid) session = cls.get_db() exec_result = await session.execute(del_stmt) - return exec_result.rowcount() + return exec_result.rowcount @classmethod async def delete_by_ids(cls, pids)-> int: session = cls.get_db() del_stmt = cls.model.delete().where(cls.model.id.in_(pids)) result = await session.execute(del_stmt) - return result.rowcount() + return result.rowcount @classmethod async def get_data_count(cls, query_params: dict = None) -> int: