How to avoid None value for optional parameters #4151
-
|
Hi, In my code i have two optional parameters in my get request , Sometimes I may pass both and sometime I may pass only one. But when i pass only 1 parameter its being consider as NULL in my query and the results are not accurate. Could anybody help how to solve this issue. i could find an option mongoengine.queryset.visitor import Q when the database is mongoDB , but not able to figure out the same for postgres db `from fastapi import FastAPI, Depends, Path,Query app = FastAPI() SqlAlchemy SetupSQLALCHEMY_DATABASE_URL = 'postgresql://{}:{}@{}:{}/{}?sslmode={}'.format('', 'prefer') def get_db(): A SQLAlchemny ORM Placeclass DBPlace(Base): Base.metadata.create_all(bind=engine) A Pydantic Placeclass Place(BaseModel): Methods for interacting with the databasedef get_place(db: Session, place_id: int, coffee: bool): def get_places(db: Session): def create_place(db: Session, place: Place): Routes for interacting with the API@app.post('/places/', response_model=Place) @app.get('/places/', response_model=List[Place]) @app.get('/query_here/') |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
hi, sorry this will be out of topic, try to use 3 ticks for code block, this will help us to read your question ```python
<big code>
``` |
Beta Was this translation helpful? Give feedback.
-
|
Hey @BonuV. In general, I'd recommend setting default non-null values for query parameters. For instance, you could give default values like Another option is to change your SQLAlchemy query; only filter by parameters provided. Your filter could look something like this: Multiple Best, |
Beta Was this translation helpful? Give feedback.
Hey @BonuV.
In general, I'd recommend setting default non-null values for query parameters. For instance, you could give default values like
coffee: Optional[bool] = False. This ensures the endpoint offers well understood, default functionality when query parameters aren't provided.Another option is to change your SQLAlchemy query; only filter by parameters provided. Your filter could look something like this:
Multiple
…