# Best pagination ordering and filtering practices.

**URL:** <https://discourse.nameko.io/t/best-pagination-ordering-and-filtering-practices/171>\
**Category:** googlegroup\
**Created:** [February 17, 2017, 7:17am UTC](https://discourse.nameko.io/t/best-pagination-ordering-and-filtering-practices/171 "2017-02-17T07:17:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trikster1911](https://avatars.discourse-cdn.com/v4/letter/t/65b543/32.png) [@Trikster1911](https://discourse.nameko.io/u/Trikster1911)\
**Post date:** [February 17, 2017, 7:17am UTC](https://discourse.nameko.io/t/best-pagination-ordering-and-filtering-practices/171/1 "2017-02-17T07:17:25Z")

</div>

Hello there! We're currently in a process of rewriting our monolithic  
django web applications into a set of nameko-based microservices. We  
heavily utilized django-rest-framework URL filters which allowed us to  
build pretty complex filters to query our models, and now in order to avoid  
rewriting tons and tons of front-end code we want to remain the same  
interface. So the question is what is the best practice for passing  
pagination, filtering and ordering parameters to nameko RPC calls? We are  
now using SQLAlchemy ORM and a layer of Repository classes providing data  
access layer, and still don't really get how to link together our "API  
Gateway" and nameko RPC calls in terms of pagination, filtering, etc.

---

<div class="post-metadata">

**Author:** ![Julio\_Trigo](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@Julio\_Trigo](https://discourse.nameko.io/u/Julio_Trigo)\
**Post date:** [February 17, 2017, 4:26pm UTC](https://discourse.nameko.io/t/best-pagination-ordering-and-filtering-practices/171/2 "2017-02-17T16:26:29Z")

</div>

Hello! We use sqlalchemy-filters for that. It's a library that we built for  
passing SQLAlchemy filtering, pagination and sorting to Nameko endpoints.  
You can just call your RPC endpoint by doing something like this:

&nbsp;&nbsp;&nbsp;&nbsp;filters = [{'field': 'name', 'op': '==', 'value': 'name\_1'}]  
&nbsp;&nbsp;&nbsp;&nbsp;order\_by = [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{'field': 'name', 'direction': 'asc'},  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{'field': 'id', 'direction': 'desc'},  
&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;page\_number=1  
&nbsp;&nbsp;&nbsp;&nbsp;page\_size=10

&nbsp;&nbsp;&nbsp;&nbsp;rcp\_service.my\_endpoint(filters, order\_by, page\_number, page\_size)

And then, use sqlalchemy-filters in my\_endpoint to perform the filtering,  
pagination and sorting.

&nbsp;&nbsp;&nbsp;&nbsp;from sqlalchemy\_filters import apply\_filters, apply\_pagination,  
apply\_sort

&nbsp;&nbsp;&nbsp;&nbsp;# existing SQLAlchemy `query` object

&nbsp;&nbsp;&nbsp;&nbsp;query = apply\_filters(query, filters)  
&nbsp;&nbsp;&nbsp;&nbsp;query = apply\_sort(query, order\_by)  
&nbsp;&nbsp;&nbsp;&nbsp;query, pagination = apply\_pagination(query, page\_number=1, page\_size=10)

The sqlalchemy-filters API accepts a SQLAlchemy query object as an argument  
so that you can apply your filters to an already filtered query.

You can have a look at it here:

> **[sqlalchemy-filters](https://pypi.org/project/sqlalchemy-filters/)**
>
> A library to filter SQLAlchemy queries.

I hope this helps!  
Julio

> **···**
>
> On Friday, 17 February 2017 07:17:26 UTC, trikst...@gmail.com wrote:
> 
> > Hello there! We're currently in a process of rewriting our monolithic  
> > django web applications into a set of nameko-based microservices. We  
> > heavily utilized django-rest-framework URL filters which allowed us to  
> > build pretty complex filters to query our models, and now in order to avoid  
> > rewriting tons and tons of front-end code we want to remain the same  
> > interface. So the question is what is the best practice for passing  
> > pagination, filtering and ordering parameters to nameko RPC calls? We are  
> > now using SQLAlchemy ORM and a layer of Repository classes providing data  
> > access layer, and still don't really get how to link together our "API  
> > Gateway" and nameko RPC calls in terms of pagination, filtering, etc.
