python - list attribute has no order by -
i tried sorting in sqlalchemy query, parameters come 'query_sort' contains list of sort parameter (field , direction).
here code
def select_all(self, query_paging, query_sort): """ method select transport type""" try: select_all_query =\ self._session.query(transporttype) s in query_sort: select_all_query =\ >> select_all_query.order_by(s.dir(getattr(transporttype, s.field)))\ .limit(query_paging.page_size)\ .offset(query_paging.skip)\ .all() return select_all_query except noresultfound: return none
then in py.test, tried test program using code :
s1 = sort s1.field = "type" s1.dir = asc s2 = sort s2.field = "transport_type_id" s2.dir = asc query_sort = [s1,s2] query_paging.skip = 1 query_paging.page_size = 10 transport_types = repo.select_all(query_paging, query_sort) assert len(transport_types) == 1
when ran test, got error :
e attributeerror: 'list' object has no attribute 'order_by'
it worked fine when using 1 data (s1) when try test using more 1 data produces error.
you called .all()
on query, returns results of query in list:
select_all_query =\ select_all_query.order_by(s.dir(getattr(transporttype, s.field)))\ .limit(query_paging.page_size)\ .offset(query_paging.skip)\ .all()
so next iteration of loop select_all_query
list.
if need apply different orderings, just that in loop:
select_all_query = self._session.query(transporttype) s in query_sort: select_all_query = select_all_query.order_by( s.dir(getattr(transporttype, s.field))) select_all_query = ( select_all_query.limit(query_paging.page_size) .offset(query_paging.skip) .all()) return select_all_query
Comments
Post a Comment