DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Odoo Pagination
Mosab Awad
Mosab Awad

Posted on

Odoo Pagination

So now you're an Odoo developer, Congratulations on that. you're one of the majority of software developers who save lives Now.
Any ways one day the tech team leader came to you asking for a favor, the Mobile application team is asking for a controller function so they can display the Purchase Orders in the application, you think to your self no worries i can handle this like a champ, All you have to do is as always:

basic api function

You did your commit and merged into the code, congrats you're done for today!. but unfortunately the Mobile application team are complaining, the PO's records that match the domain of search are more than a thousand records. The performance of the function is lacking speed, the payload is heavy and they don't want to waste more time in filtering data when display in the application.

You sipped your coffee (Star box because you're rich now) and thought about a way to return less data but you don't want to miss records from the domain. hum! how to do that, Ok let's do it together and think out loud:

You need to speed up the search so you need to limit the number of records every time, but also you want to make the filtering easier for the Mobile team. so you have to divide data into pages.

That way you're not missing any data from the same domain of search and also data are in different pages so they are ordered and ready to be displayed.

To implement this trick you need two Odoo ORM Search function parameters:

1 - limit: which is simply the number of records you want to return from the search, by default it's returning all the records.
2 - offset: the number of records to ignore at first.

*Confused? lets illustrate this:
*

Offset Illustration

so let's assume you set offset=2 so the search will ignore the first two records and start from record number 3.

next let's use the limit parameter and illustrate it:

Limit Illustration

you notice the limit=5 so the search will stop at the record number 7.

now you can understand the final shape i want from both of the tools. i want to divide every group of records into page and every page have a limited number of records, in order to get this shape:

Pagination Illustration

so now if i want data from page 4 i know that i have to ignore the first three pages using offset. you get the point? now lets start your favorite part .. the code of course:

full pagination

so now back to your mobile application team with an email asking them to send two parameter:
1 - page_number
2 - record_per_page

considering the example we had before, let's say thy want 4 records in every page and they want to display page number 3. all you have to do is to make the offset ignore the first two pages, and what is that equal to? YES the number of the records in the previous two pages.
so the equation is going to be:
( the current requested page - 1 ) * the number of records in one page.
so the offset of the third page is (3 - 1) * 4 = 8.
with the limits set to 4 we are going to get exactly the data in the fourth page.

CONGRATULATIONS!! your function is now working with pagination. the payload is no longer too large for the Mobile application and the team don't have to filter the data on display so the application is now much faster.

YOU SAVED LIVES AGAIN ...

Top comments (1)

Collapse
 
ahmedsaid95 profile image
ahmedsaid95

Nice way to explain the issues

Timeless DEV post...

Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.