9

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?

cglacet
  • 6,638
  • 2
  • 35
  • 49

2 Answers2

11

The documentation gives a shortcut to avoid this kind of repetitions. In this case, it would give:

from fastapi import Depends

@app.post("/test-query-params")
def test_query(model: Model = Depends()): pass

This will allow you to request /test-query-params?x=1&y=2 and will also produce the correct OpenAPI description for this endpoint.

Similar solutions can be used for using Pydantic models as form-data descriptors.

JPG
  • 69,639
  • 12
  • 88
  • 162
cglacet
  • 6,638
  • 2
  • 35
  • 49
  • Wooow, thank you SO much! This has eluded me for months! It's unintuitive that `model: Model` is recognized as body params, but `model: Model = Depends()` is recognized as query params. The fastapi docs are nice and all, but sometimes they fail to highlight key things like this. – mblakesley May 04 '21 at 05:54
  • @mblakesley don't hesitate asking for help on the dedicated gitter, I think that's how I got this information (https://gitter.im/tiangolo/fastapi) – cglacet May 07 '21 at 19:07
1

This solution is very apt if your schema is "minimal".

But, when it comes to a complicated one like this, Set description for query parameter in swagger doc using Pydantic model, it is better to use a "custom dependency class"

from fastapi import Depends, FastAPI, Query

app = FastAPI()


class Model:
    def __init__(
            self,
            y: str,
            x: str = Query(
                default='default for X',
                title='Title for X',
                deprecated=True
            )

    ):
        self.x = x
        self.y = y


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

If you are using this method, you will have more control over the OpenAPI doc.

JPG
  • 69,639
  • 12
  • 88
  • 162
  • Are you aware you can also use `Query` definitions in pydantic models? – h345k34cr Oct 20 '20 at 09:45
  • @h345k34cr Yes, I do. Unfortunately, it doesn't work for the ***linked SO post*** – JPG Oct 20 '20 at 09:51
  • It is also worth to add a minimal example here as a new answer so that someone else can benefit from that. @h345k34cr – JPG Oct 20 '20 at 09:54