Fixed sort_query hybrid property handling

This commit is contained in:
Konsta Vesterinen
2013-09-19 15:25:09 +03:00
parent b1905c2d8a
commit 22562ac4cc
2 changed files with 11 additions and 2 deletions

View File

@@ -20,6 +20,15 @@ def sort_expression(expr, attr_name):
return getattr(expr, attr_name)
def get_entity(expr):
if isinstance(expr, AliasedInsp):
return expr.mapper.class_
elif isinstance(expr, Mapper):
return expr.class_
else:
return expr
def matches_entity(alias, entity):
if not alias:
return True
@@ -85,6 +94,7 @@ class QuerySorter(object):
))
# Check hybrid properties.
entity = get_entity(entity)
if hasattr(entity, sort['attr']):
return self.query.order_by(
sort['func'](getattr(entity, sort['attr']))

View File

@@ -129,5 +129,4 @@ class TestSortQuery(TestCase):
)
)
query = sort_query(query, '-category-articles_count')
print query
#assert 'ORDER BY (SELECT count(article.id) AS count_1' in str(query)
assert 'ORDER BY (SELECT count(article.id) AS count_1' in str(query)