I am creating an application with FastAPI and I want to filter 'items' in an endpoint according to their name and category. However, I want that if any of these two fields is not passed, it is not taken into account, and if both are passed, both filters are applied. I have written the following code for this:
async def filter_items_by_name_category(
        self,
        name: str | None = None,
        category: str | None = None
    ):
        if name is not None:
            items_by_name = await self.model.filter(name__icontains = name).all()
            if category is None:
                return items_by_name
        if category is not None:
            items_by_category = await self.model.filter(category = category).all()
            if name is None:
                return items_by_category
        items = [item for item in items_by_name if item in items_by_category]
        return items
The code works but it seems very inelegant to me! I was thinking that maybe there is another better implementation. I am using tortoise orm but I think it will not have relevance in the code.
 
     
     
    