Open mmzeynalli opened 1 month ago
Hmmm,
You can try to use transformer
:
paginate(
query,
transformer=lambda items: [{**obj, "is_saved": is_saved} for obj, is_saved in items],
)
The obj
is Object object, meaning, it is not dict. I was able to do a workaround like this which could be maybe adopted?
def merge_agg_to_obj(item: Row):
extra = item._asdict() # main_obj + extra
obj = extra.pop(item._parent._keys[0])
# TODO: Fix this workaround
obj.__dict__.update(extra)
return obj
def unwrap_with_merging_agg_to_obj(items: Sequence[Row]):
return [merge_agg_to_obj(item[0] if len_or_none(item) == 1 else item) for item in items]
For now, to use it, I need to patch the internal function like:
def paginate_with_merge(db: Session, query: Select):
import fastapi_pagination.ext.sqlalchemy as fp_sa
fp_sa.unwrap_scalars = unwrap_with_merging_agg_to_obj
res = fp_sa.paginate(db, query)
fp_sa.unwrap_scalars = unwrap_scalars
return res
Hi @mmzeynalli,
Sorry for long response, I guess you can achieve it without patching unwrap_scalars
.
Can you try to use transformer like this?
def transformer(items: list[tuple[Object, int]]) -> list[Object]:
def _transformer(obj: Object, is_saved: bool) -> Object:
obj.is_saved = is_saved
return obj
return [_transformer(obj, is_saved) for obj, is_saved in items]
So, I have the following query:
This produces a tuple of (Object, int). To paginate this query, I need to have the schema:
which works. However, I want
is_saved
to be part ofObjectMinimalSchema
:and use this schema for pagination.