Skip to content

Commit

Permalink
Fix: sort list when PK is not 'id'
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaxxl committed Aug 22, 2024
1 parent c388607 commit d68e82f
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion safrs/jsonapi_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,15 @@ def jsonapi_sort(object_query, safrs_object):
if attr is None:
if safrs_object.id_type.primary_keys:
attr = getattr(safrs_object, safrs_object.id_type.primary_keys[0]) # todo: composite keys edge case
sort_attr = attr.name
else:
continue
elif attr is None or sort_attr not in safrs_object._s_jsonapi_attrs:
safrs.log.debug(f"{safrs_object} has no attribute {sort_attr} in {safrs_object._s_jsonapi_attrs}")
continue
if isinstance(object_query, (list, sqlalchemy.orm.collections.InstrumentedList)):
object_query = sorted(
list(object_query), key=lambda obj: (getattr(obj, sort_attr) is None, getattr(obj, sort_attr)), reverse=reverse
list(object_query), key=lambda obj: (getattr(obj, sort_attr, None) is None, getattr(obj, sort_attr, None)), reverse=reverse
)
elif is_jsonapi_attr(attr):
# to do: implement sorting for jsonapi_attr
Expand Down Expand Up @@ -248,3 +249,4 @@ def jsonapi_format_response(data=None, meta=None, links=None, errors=None, count
result["included"] = safrs.base.Included

return result

0 comments on commit d68e82f

Please sign in to comment.