From 3fb3c2e24d9121e06a5c0294d5bdaa16cde06c8f Mon Sep 17 00:00:00 2001 From: Jakub Nowakowski Date: Fri, 12 Apr 2024 16:43:44 +0200 Subject: [PATCH] skip count call when possible --- fastcrud/crud/fast_crud.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/fastcrud/crud/fast_crud.py b/fastcrud/crud/fast_crud.py index c9f9044..3b6d91e 100644 --- a/fastcrud/crud/fast_crud.py +++ b/fastcrud/crud/fast_crud.py @@ -1358,8 +1358,7 @@ async def update( update(db, {'username': 'new_username'}, id__ne=1, allow_multiple=False) ``` """ - total_count = await self.count(db, **kwargs) - if not allow_multiple and total_count > 1: + if not allow_multiple and (total_count := await self.count(db, **kwargs)) > 1: raise MultipleResultsFound( f"Expected exactly one record to update, found {total_count}." ) @@ -1423,8 +1422,7 @@ async def db_delete( db_delete(db, username='unique_username', allow_multiple=False) ``` """ - total_count = await self.count(db, **kwargs) - if not allow_multiple and total_count > 1: + if not allow_multiple and (total_count := await self.count(db, **kwargs)) > 1: raise MultipleResultsFound( f"Expected exactly one record to delete, found {total_count}." )