[Python] Query parameters from pydantic model

Discussão em 'Python' iniciado por Stack, Fevereiro 11, 2025.

  1. Stack

    Stack Membro Participativo

    Is there a way to convert a pydantic model to query parameters in fastapi?

    Some of my endpoints pass parameters via the body, but some others pass them directly in the query. All this endpoints share the same data model, for example:

    class Model(BaseModel):
    x: str
    y: str


    I would like to avoid duplicating my definition of this model in the definition of my "query-parameters endpoints", like for example test_query in this code:

    class Model(BaseModel):
    x: str
    y: str

    @app.post("/test-body")
    def test_body(model: Model): pass

    @app.post("/test-query-params")
    def test_query(x: str, y: str): pass


    What's the cleanest way of doing this?

    Continue reading...

Compartilhe esta Página