I want to do 3 operations on a table which contains large number of records: (1)filter
(based on a search query) (2)order_by
(on a single column in asc/desc) and (3)slice
(for given offset
and limit
values).
While carrying out these operations I need the intermediate result (obtained after filtering/sorting), to find out the number of records which satisfied the given filter query (to send this information to the front-end).
Right now I am using FILTER
and ORDER_BY
of sqlalchemy to get the intermediate result and then applying slicing on the list. How can I achieve the same using sqlalchemy's FILTER
, ORDER_BY
and SLICE
together along with getting the number of records after filtering/sorting as a sub-result?
The sqlalchemy query I am using right now is as follows:
result = session.\
query(Customer).\
filter(Customer.name.like('%' + filter_query + '%') | Customer.email.like('%' + filter_query + '%') | Customer.partner.like('%' + filter_query + '%')).\
order_by(asc(getattr(Customer, sorting_column_name))).\
all()
result = session.\
query(Customer).\
filter(Customer.name.like('%' + filter_query + '%') | Customer.email.like('%' + filter_query + '%') | Customer.partner.like('%' + filter_query + '%')).\
order_by(asc(getattr(Customer, sorting_column_name))).\
slice(offset,limit).\
all()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With