Skip to content
This repository was archived by the owner on Dec 16, 2025. It is now read-only.

Commit d22d595

Browse files
committed
bug fix to allow sorting on hybrid_property
1 parent f2cb7e9 commit d22d595

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

flask_rest_jsonapi/data_layers/alchemy.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from sqlalchemy.orm.exc import NoResultFound
44
from sqlalchemy.orm.collections import InstrumentedList
5-
from sqlalchemy.sql.expression import desc, asc, text
65
from sqlalchemy.inspection import inspect
76

87
from flask_rest_jsonapi.constants import DEFAULT_PAGE_SIZE
@@ -425,15 +424,12 @@ def sort_query(self, query, sort_info):
425424
:param list sort_info: sort information
426425
:return Query: the sorted query
427426
"""
428-
expressions = {'asc': asc, 'desc': desc}
429-
order_objects = []
430427
for sort_opt in sort_info:
431-
if not hasattr(self.model, sort_opt['field']):
432-
raise InvalidSort("{} has no attribute {}".format(self.model.__name__, sort_opt['field']))
433-
field = text(sort_opt['field'])
434-
order = expressions[sort_opt['order']]
435-
order_objects.append(order(field))
436-
return query.order_by(*order_objects)
428+
field = sort_opt['field']
429+
if not hasattr(self.model, field):
430+
raise InvalidSort("{} has no attribute {}".format(self.model.__name__, field))
431+
query = query.order_by(getattr(getattr(self.model, field), sort_opt['order'])())
432+
return query
437433

438434
def paginate_query(self, query, paginate_info):
439435
"""Paginate query according to jsonapi 1.0

0 commit comments

Comments
 (0)