-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2024年12月30日 整理SQLAlchemy和pydantic 大版本升级问题
- Loading branch information
Showing
14 changed files
with
26 additions
and
723 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
#!/usr/bin/env python | ||
# -*-coding:utf-8-*- | ||
""" | ||
Author : shenshuo | ||
Date : 2019年12月11日 | ||
Contact : [email protected] | ||
Author : shenshuo | ||
Date : 2024/12/30 20:28 | ||
Desc : models类 | ||
""" | ||
|
||
|
@@ -35,30 +36,6 @@ def queryset_to_list(queryset, **kwargs) -> list: | |
return [model_to_dict(q) for q in queryset] | ||
|
||
|
||
def GetInsertOrUpdateObj(cls: Type, str_filter: str, **kw) -> classmethod: | ||
""" | ||
cls: Model 类名 | ||
str_filter: filter的参数.eg:"name='name-14'" 必须设置唯一 支持 and or | ||
**kw: 【属性、值】字典,用于构建新实例,或修改存在的记录 | ||
session.add(GetInsertOrUpdateObj(TableTest, "name='name-114'", age=33114, height=123.14, name='name-114')) | ||
""" | ||
with DBContext('r') as session: | ||
existing = session.query(cls).filter(text(str_filter)).first() | ||
if not existing: | ||
res = cls() | ||
for k, v in kw.items(): | ||
if hasattr(res, k): | ||
setattr(res, k, v) | ||
return res | ||
else: | ||
res = existing | ||
for k, v in kw.items(): | ||
if hasattr(res, k): | ||
setattr(res, k, v) | ||
|
||
return res | ||
|
||
|
||
def insert_or_update(cls: Type[DeclarativeMeta], str_filter: str, **kw) -> Union[None, DeclarativeMeta]: | ||
""" | ||
cls: Model 类名 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,18 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Version : 0.0.8 | ||
Version : 0.0.9 | ||
Contact : [email protected] | ||
Author : shenshuo | ||
Date : 2021/1/26 20:28 | ||
Desc : https://github.com/tiangolo/pydantic-sqlalchemy | ||
Date : 2024/12/30 20:28 | ||
Desc : https://github.com/tiangolo/pydantic-sqlalchemy | ||
""" | ||
|
||
#### | ||
from typing import Container, Optional, Type | ||
from pydantic import BaseModel, create_model, ValidationError, ConfigDict | ||
|
||
|
||
# from sqlalchemy.inspection import inspect | ||
# from sqlalchemy.orm.properties import ColumnProperty | ||
|
||
|
||
# 删除的时候一般只有id | ||
class PydanticDel(BaseModel): | ||
id: int | ||
|
@@ -26,7 +22,7 @@ class PydanticDelList(BaseModel): | |
id_list: list[int] | ||
|
||
|
||
def sqlalchemy_to_pydantic(db_model: Type, *, exclude: Container[str] = []) -> Type[BaseModel]: | ||
def sqlalchemy_to_pydantic(db_model: Type, *, exclude: Container[str] = ()) -> Type[BaseModel]: | ||
table = db_model.metadata.tables[db_model.__tablename__] | ||
fields = {} | ||
config = ConfigDict(from_attributes=True) | ||
|
@@ -41,9 +37,9 @@ def sqlalchemy_to_pydantic(db_model: Type, *, exclude: Container[str] = []) -> T | |
elif hasattr(column.type, "python_type"): | ||
python_type = column.type.python_type | ||
assert python_type, f"Could not infer python_type for {column}" | ||
default = None | ||
if column.default is None and not column.nullable: | ||
default = ... | ||
|
||
default = ... if column.default is None and not column.nullable else None | ||
|
||
fields[name] = (python_type, default) | ||
pydantic_model = create_model(db_model.__name__, __config__=config, **fields) | ||
return pydantic_model | ||
|
||
return create_model(db_model.__name__, __config__=config, **fields) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.