Compare commits
2 Commits
main
...
masenf/mod
Author | SHA1 | Date | |
---|---|---|---|
![]() |
61aca9d794 | ||
![]() |
3253088700 |
@ -261,19 +261,32 @@ class Model(Base, sqlmodel.SQLModel): # pyright: ignore [reportGeneralTypeIssue
|
|||||||
super().__init_subclass__()
|
super().__init_subclass__()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _dict_recursive(cls, value):
|
def _dict_recursive(cls, value, seen_objects: set | None = None):
|
||||||
"""Recursively serialize the relationship object(s).
|
"""Recursively serialize the relationship object(s).
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
value: The value to serialize.
|
value: The value to serialize.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The serialized value.
|
The serialized value,
|
||||||
|
or Empty value when the relationship cannot be followed,
|
||||||
|
or None if the value has already been serialized in this descent.
|
||||||
"""
|
"""
|
||||||
if hasattr(value, "dict"):
|
if seen_objects is None:
|
||||||
return value.dict()
|
seen_objects = set()
|
||||||
elif isinstance(value, list):
|
if id(value) in seen_objects:
|
||||||
return [cls._dict_recursive(item) for item in value]
|
return None
|
||||||
|
seen_objects.add(id(value))
|
||||||
|
try:
|
||||||
|
if hasattr(value, "dict"):
|
||||||
|
return value.dict()
|
||||||
|
except sqlalchemy.orm.exc.DetachedInstanceError:
|
||||||
|
return {}
|
||||||
|
if isinstance(value, list):
|
||||||
|
return [
|
||||||
|
cls._dict_recursive(item, seen_objects=seen_objects)
|
||||||
|
for item in value
|
||||||
|
]
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def dict(self, **kwargs):
|
def dict(self, **kwargs):
|
||||||
@ -533,6 +546,7 @@ def asession(url: str | None = None) -> AsyncSession:
|
|||||||
_AsyncSessionLocal[url] = sqlalchemy.ext.asyncio.async_sessionmaker(
|
_AsyncSessionLocal[url] = sqlalchemy.ext.asyncio.async_sessionmaker(
|
||||||
bind=get_async_engine(url),
|
bind=get_async_engine(url),
|
||||||
class_=AsyncSession,
|
class_=AsyncSession,
|
||||||
|
expire_on_commit=False,
|
||||||
autocommit=False,
|
autocommit=False,
|
||||||
autoflush=False,
|
autoflush=False,
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user