CRUDkit fixes and changes.

This commit is contained in:
Yaro Kasear 2025-09-10 09:18:31 -05:00
parent 9d36d600bb
commit 7ddfe084ba
5 changed files with 98 additions and 43 deletions

View file

@ -81,8 +81,8 @@ class CRUDService(Generic[T]):
for eager in spec.get_eager_loads(root_alias, fields_map=rel_field_names):
query = query.options(eager)
if root_fields or rel_field_names:
query = query.options(Load(root_alias).raiseload("*"))
# if root_fields or rel_field_names:
# query = query.options(Load(root_alias).raiseload("*"))
if filters:
query = query.filter(*filters)
@ -98,26 +98,43 @@ class CRUDService(Generic[T]):
# Only apply offset/limit when not None.
if offset is not None and offset != 0:
query = query.offset(offset)
if limit is not None:
if limit is not None and limit > 0:
query = query.limit(limit)
# return query.all()
rows = query.all()
try:
rf_names = [c.key for c in (root_fields or [])]
except NameError:
rf_names = []
if rf_names:
allow = set(rf_names)
if "id" not in allow and hasattr(self.model, "id"):
allow.add("id")
proj = []
if root_fields:
proj.extend(c.key for c in root_fields)
for path, names in (rel_field_names or {}).items():
prefix = ".".join(path)
for n in names:
proj.append(f"{prefix}.{n}")
if proj and "id" not in proj and hasattr(self.model, "id"):
proj.insert(0, "id")
if proj:
for obj in rows:
try:
setattr(obj, "__crudkit_root_fields__", allow)
setattr(obj, "__crudkit_projection__", tuple(proj))
except Exception:
pass
# try:
# rf_names = [c.key for c in (root_fields or [])]
# except NameError:
# rf_names = []
# if rf_names:
# allow = set(rf_names)
# if "id" not in allow and hasattr(self.model, "id"):
# allow.add("id")
# for obj in rows:
# try:
# setattr(obj, "__crudkit_root_fields__", allow)
# except Exception:
# pass
return rows
def create(self, data: dict, actor=None) -> T: