Skip to content

Adding an extra filter (in addition to item_id) to the update() method of the service / repository #135

Answered by JacobCoffee
shrir asked this question in Q&A
Discussion options

You must be logged in to vote

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:

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

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@JacobCoffee
Comment options

Answer selected by shrir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants