full-stack-fastapi-template
full-stack-fastapi-template copied to clipboard
how do you make "get_item" search query using string ?
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?
read this https://amitosh.medium.com/full-text-search-fts-with-postgresql-and-sqlalchemy-edc436330a0c