question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Merge Path + Query Parameters in BaseModel

See original GitHub issue

Hi all,

I just started using fastapi and I’m struggling to understant how to use the BaseModel on both path + query parameters.

my endpoint:

rs = client.get("/data/a/b/c")
# possibly
rs = client.get("/data/a/b/c?sort=true&page=1")

On the code below is there a way to make the item return all the optional values declared in the Model?

class Item(BaseModel):
    sort: Optional[str] =  ""
    page: Optional[int] = 1
    per_page: Optional[int] = 20
    search_term: Optional[str] = ""

@app.get("/data/{l1}/{l2}/{l3}")
def d_tree(
           l1: str,
           l2: str,
           l3: str,
           item: Optional[Item] = None):
    print(l1, l2, l3) # l1 l2 l3 ok
    print(item) # None

This would throw:

@app.get("/data/{l1}/{l2}/{l3}")
def d_tree(*,
           l1: str,
           l2: str,
           l3: str,
           item: Item):
#{'detail': [{'loc': ['body'], 'msg': 'field required', 'type': 'value_error.missing'}]}

I’m wondering if It’s possible to get all my variables inside the Model like this:

class Item(BaseModel):
    l1: str
    l2: str
    l3: str
    sort: Optional[str] =  ""
    page: Optional[int] = 1
    per_page: Optional[int] = 20
    search_term: Optional[str] = ""

@app.get("/data/{l1}/{l2}/{l3}")
def d_tree(item: Item):
    toil.phead(item)

    return []

Result

{'detail': [{'loc': ['body'], 'msg': 'field required', 'type': 'value_error.missing'}]}

Expect

{   
    'l1': 'a',
    'l2': 'b',
    'l3': 'c',
    'page': 1,
    'per_page': 20,
    'search_term': '',
    'sort': ''
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
tiangolocommented, Nov 9, 2022

Thanks for the help here @jrversteegh! 🚀

And thanks @CrashLaker for coming back to close the issue.

Sorry for the long delay! 🙈 I wanted to personally address each issue/PR and they piled up through time, but now I’m checking each one in order.

0reactions
jrversteeghcommented, Oct 12, 2020

np and you’re welcome.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Body - Multiple Parameters - FastAPI
First, of course, you can mix Path , Query and request body parameter ... fastapi import FastAPI, Path from pydantic import BaseModel app...
Read more >
How to implement dynamic API filtering using query ... - Mindee
When it comes to API filtering, the most intuitive way of doing this is to use query parameters. Query parameters only having a...
Read more >
Swagger: Combine Path and Query Parameters in Same ...
Can you use both a path parameter and a query parameter in Swagger 2.0 or 3.0? For example, given the following base URL...
Read more >
2.0 Breaking Changes | FeathersVuex
Using custom query parameters. There are two places where the query operators have to be allowed. In the Feathers Client (for the actions):...
Read more >
tiangolo/fastapi - Gitter
Does anyone know how to use a pydantic BaseModel as query parameter? I can't find this information in the documentation. I would like...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found