From 95b343969faf2d1b498e0ac8efdeb1393fa69b70 Mon Sep 17 00:00:00 2001 From: Ed-XCF Date: Thu, 25 Nov 2021 17:44:59 +0800 Subject: [PATCH] add _asdict to support fastapi-pagination --- bali/db/connection.pyi | 12 +++++++++++- bali/db/models.py | 5 ++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/bali/db/connection.pyi b/bali/db/connection.pyi index 076f25f..2f2891f 100644 --- a/bali/db/connection.pyi +++ b/bali/db/connection.pyi @@ -1,5 +1,5 @@ from datetime import datetime -from typing import Dict, Optional, Any, Type, TypeVar, Union, List +from typing import Dict, Optional, Any, Type, TypeVar, Union, List, Tuple from sqlalchemy.orm.query import Query from sqlalchemy.orm.session import Session @@ -17,8 +17,12 @@ class BaseModel: updated_time: _DateTimeField is_active: _BooleanField + def _asdict(self) -> Dict[str, Any]: ... + def to_dict(self) -> Dict[str, Any]: ... + def dict(self) -> Dict[str, Any]: ... + @classmethod def exists(cls: Type[_M], **attrs) -> bool: ... @@ -51,6 +55,12 @@ class BaseModel: @classmethod def get_fields(cls: Type[_M]) -> List[str]: ... + @classmethod + def get_or_create(cls: Type[_M], defaults: Dict[str, Any], **attrs) -> Tuple[_M, bool]: ... + + @classmethod + def update_or_create(cls: Type[_M], defaults: Dict[str, Any], **attrs) -> Tuple[_M, bool]: ... + class DB(SQLAlchemy): BaseModel: BaseModel diff --git a/bali/db/models.py b/bali/db/models.py index 4ca2dee..0c4a4eb 100644 --- a/bali/db/models.py +++ b/bali/db/models.py @@ -95,11 +95,10 @@ def delete(self): db.session.delete(self) db.session.commit() if context_auto_commit.get() else db.session.flush() - def to_dict(self): + def _asdict(self): return {c.name: getattr(self, c.name, None) for c in self.__table__.columns} - def dict(self): - return self.to_dict() + dict = to_dict = _asdict @classmethod def count(cls, **attrs) -> int: