From e8bd4ba050a68ad42826894e51c32d81a46a916e Mon Sep 17 00:00:00 2001 From: Vishal Khare Date: Sat, 25 Jun 2022 20:07:22 +0530 Subject: [PATCH] Fix: Limit not working on execute() Problem? Often time in production we don't want to return all matching records instead returning a paginated response based on values of limit and offset is desirable. What is being fixed? Even after setting a limit value it was not working and .all() or .execute() function were returning complete set of records matching the search criterion. How is it fixed? To break away from loop executing the query a condition is added wherein if total number of fetched records is equal or greater than limit. If no limit is provided, default value i.e. 10 records will be returned. --- aredis_om/model/model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aredis_om/model/model.py b/aredis_om/model/model.py index 92bb6f9a..da8fcaf1 100644 --- a/aredis_om/model/model.py +++ b/aredis_om/model/model.py @@ -742,7 +742,7 @@ async def execute(self, exhaust_results=True): # current offset plus `page_size`, until we stop getting results back. query = query.copy(offset=query.offset + query.page_size) _results = await query.execute(exhaust_results=False) - if not _results: + if not _results or len(self._model_cache) >= query.limit: break self._model_cache += _results return self._model_cache