Makign corrections on field selection.
This commit is contained in:
parent
7e915423a3
commit
9d36d600bb
9 changed files with 1504 additions and 39 deletions
|
|
@ -9,13 +9,26 @@ class CRUDMixin:
|
|||
created_at = Column(DateTime, default=func.now(), nullable=False)
|
||||
updated_at = Column(DateTime, default=func.now(), nullable=False, onupdate=func.now())
|
||||
|
||||
def as_dict(self):
|
||||
# Combine all columns from all inherited tables
|
||||
def as_dict(self, fields: list[str] | None = None):
|
||||
"""
|
||||
Serialize mapped columns. Honors projection if either:
|
||||
- 'fields' is passed explicitly, or
|
||||
-
|
||||
"""
|
||||
allowed = None
|
||||
if fields:
|
||||
allowed = set(fields)
|
||||
else:
|
||||
allowed = getattr(self, "__crudkit_root_fields__", None)
|
||||
result = {}
|
||||
for cls in self.__class__.__mro__:
|
||||
if hasattr(cls, "__table__"):
|
||||
for column in cls.__table__.columns:
|
||||
result[column.name] = getattr(self, column.name)
|
||||
if not hasattr(cls, "__table__"):
|
||||
continue
|
||||
for column in cls.__table__.columns:
|
||||
name = column.name
|
||||
if allowed is not None and name not in allowed and name != "id":
|
||||
continue
|
||||
result[name] = getattr(self, name)
|
||||
return result
|
||||
|
||||
class Version(Base):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue