[QUESTION] Using pydantic models for GET request query params? Currently not possible, have to use dataclasses or normal classes.
See original GitHub issueDescription
Is there a way to use pydantic models for GET requests? I would like to have a similar interface for both query params and for the body. So for instance, an example could look like this:
class PingArgs(BaseModel):
"""Model input for PingArgs."""
dt: datetime.datetime = ...
to_sum: List[int] = ...
@validator("dt", pre=False, always=True, whole=True)
def validate_dt(cls, v, values):
"""Validate dt."""
parsed_dt = v.replace(tzinfo=None)
return parsed_dt
@router.get("/ping", tags=["basic"])
def ping(args: PingArgs, request: Request):
"""Example."""
return JSONResponse(
status_code=starlette.status.HTTP_200_OK,
content={"detail": "pong", "dt": args.dt.isoformat() "summed": sum(x for x in args.to_sum)},
)
Where as, right now I think you would have to do something like this:
@router.get("/ping", tags=["basic"])
def ping(dt: datetime.datetime = Query(None), to_sum: List[int] = Query(None), request: Request):
"""Example."""
parsed_dt = dt.replace(tzinfo=None)
return JSONResponse(
status_code=starlette.status.HTTP_200_OK,
content={"detail": "pong", "dt": dt.isoformat() "summed": sum(x for x in to_sum)},
)
Hope this can be clarified.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:25
- Comments:42 (9 by maintainers)
Top Results From Across the Web
FastAPI - GET Request with Pydantic List field - Stack Overflow
When you declare a List field in the Pydantic model, it is interpreted as a request body parameter, instead of a query one...
Read more >Using Dataclasses - FastAPI
Using Dataclasses ¶. FastAPI is built on top of Pydantic, and I have been showing you how to use Pydantic models to declare...
Read more >Pydantic V2 Plan
pydantic -core operates on a tree of validators with no "model" type required at the base of that tree. It can therefore validate...
Read more >Pydantic or dataclasses? Why not both? Convert Between Them
To get the information we need for the pydantic model, we use the fields utility ... We can check if the class is,...
Read more >Any reason not to use dataclasses everywhere? : r/Python
Absolutely use data classes when they do the job. Cases when this is not true (or it's awkward):. custom init method. custom new...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@LasseGravesen You would do it like this:
Check the docs here: https://fastapi.tiangolo.com/tutorial/dependencies/classes-as-dependencies/
This is an older issue but I wanted to show my solution to this problem:
I don’t know if this helps anyone / solves the problem but it does allow you to use pydantic validation for query parameters and get similar error responses to payload validation failure.