full-stack-fastapi-template icon indicating copy to clipboard operation
full-stack-fastapi-template copied to clipboard

how do you make "get_item" search query using string ?

Open pramadito opened this issue 4 years ago • 1 comments

i get confused while reading the documentation, there example code for get all items, there example code to get item with id one but i found no example search database using string.

endpoints

@router.get("/search/{search}", response_model=List[schemas.HouseItem])
def search_houseitem_open(
    db: Session = Depends(deps.get_db),
    q: Optional[str] = None,
    skip: int = 0,    
    limit: int = 100,
) -> Any:
    """
    search houseitems.
    """
    houseitems = crud.houseitem.get_multi_by_search(
            db=db, q = q , skip=skip, limit=limit
        )
    
    return houseitems

crud

def get_multi_by_search(
        self, db: Session, *,  q : Optional[str] = None , skip: int = 0, limit: int = 100
    ) -> List[ModelType]:
        return (
            db.query(self.model)
            .filter()  #what filter should i use
            .offset(skip)
            .limit(limit)
            .all()
        )

is this code correct? what filter should i use so it can match case SQL “LIKE” statement?

pramadito avatar May 27 '21 09:05 pramadito

read this https://amitosh.medium.com/full-text-search-fts-with-postgresql-and-sqlalchemy-edc436330a0c

johnsonjsyuen avatar Jul 10 '21 13:07 johnsonjsyuen