From d68e82f831b4d8a1d681516e00d5d0efcbccdb97 Mon Sep 17 00:00:00 2001 From: Thomas Pollet Date: Thu, 22 Aug 2024 09:26:56 +0200 Subject: [PATCH] Fix: sort list when PK is not 'id' --- safrs/jsonapi_formatting.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/safrs/jsonapi_formatting.py b/safrs/jsonapi_formatting.py index 3e42db7..ff04eaf 100755 --- a/safrs/jsonapi_formatting.py +++ b/safrs/jsonapi_formatting.py @@ -73,6 +73,7 @@ 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: @@ -80,7 +81,7 @@ def jsonapi_sort(object_query, safrs_object): 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 @@ -248,3 +249,4 @@ def jsonapi_format_response(data=None, meta=None, links=None, errors=None, count result["included"] = safrs.base.Included return result +