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__()
|
||||
|
||||
@classmethod
|
||||
def _dict_recursive(cls, value):
|
||||
def _dict_recursive(cls, value, seen_objects: set | None = None):
|
||||
"""Recursively serialize the relationship object(s).
|
||||
|
||||
Args:
|
||||
value: The value to serialize.
|
||||
|
||||
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"):
|
||||
return value.dict()
|
||||
elif isinstance(value, list):
|
||||
return [cls._dict_recursive(item) for item in value]
|
||||
if seen_objects is None:
|
||||
seen_objects = set()
|
||||
if id(value) in seen_objects:
|
||||
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
|
||||
|
||||
def dict(self, **kwargs):
|
||||
@ -533,6 +546,7 @@ def asession(url: str | None = None) -> AsyncSession:
|
||||
_AsyncSessionLocal[url] = sqlalchemy.ext.asyncio.async_sessionmaker(
|
||||
bind=get_async_engine(url),
|
||||
class_=AsyncSession,
|
||||
expire_on_commit=False,
|
||||
autocommit=False,
|
||||
autoflush=False,
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user