Adding an extra filter (in addition to item_id) to the update() method of the service / repository #135
-
Hello, I am trying to update an item with an
How do I pass extra filters to the Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I'm not sure if you can get this natively, but you can create a method on your BookService/Repository to have a 2nd update method with this maybe. Starting off basing it off of async def filtered_update(
self,
session: AsyncSession,
data: dict[str, Any],
item_id: Any,
extra_filters: dict[str, Any],
auto_commit: bool = False
) -> Book:
query = select(self.model_type).where(self.model_type.id == item_id)
for key, value in extra_filters.items():
query = query.where(getattr(self.model_type, key) == value)
result = await session.execute(query)
instance = result.scalars().first() ...then you can do something like async with Session() as session:
extra_filters = {"name": "Litestar"}
updated_book = await book_repository.update_with_extra_filters(
session=session,
item_id="specific_book_id",
data={"title": "Litestar in a Month of Lunches"},
extra_filters=extra_filters,
auto_commit=True
) |
Beta Was this translation helpful? Give feedback.
I'm not sure if you can get this natively, but you can create a method on your BookService/Repository to have a 2nd update method with this maybe.
Starting off basing it off of
advanced_alchemy.service._async.SQLAlchemyAsyncRepositoryService.update
, something like this poorly written pseudo code: