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.

Cannot declare parameters to receive multiples values with pydantic BaseModel and Depends

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from typing import Optional, List

from fastapi import FastAPI, Depends, Query
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None
    relates_to: List[int] = Query(...)


@app.get("/items/")
async def sample_get_items(
    item: Item = Depends()
):
    return "ok"

Description

This code is based on the samples in the fastapi site.

The problem is in the parameter relates_to . This parameter not appears in the /docs as a parameter because this is of the tytpe List[int]

I need, by this way, declaring parameters for a endpoint thought a pydantic.BaseModel, that have possibility to receive multiple values.

But the way it’s written in the example, it doesn’t work, either by sending /item/?relates_to=10&relates_to=11&relates_to=12 or /item/?relates_to=[10,11,12] or other way…

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.68.1

Python Version

3.9.5

Additional Context

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
berlottocommented, Sep 22, 2021

How this #question can be transformed in a #issue ?

0reactions
berlottocommented, Oct 6, 2021

You are not supposed to have a Query inside the Pydantic model.

I undertand, but is the only way that this works in a Pydantic model (see image) to be simple

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

FastAPI - GET Request with Pydantic List field - Stack Overflow
When you declare a List field in the Pydantic model, ... and appear multiple times in the URL (in others words, to receive...
Read more >
How to Validate Your Data with Custom Validators of Pydantic ...
By default, the values argument of a validator only stores the fields that have already been validated. Which fields are available in values...
Read more >
Body - Nested Models - FastAPI
To declare types that have type parameters (internal types), like list ... Union from fastapi import FastAPI from pydantic import BaseModel app =...
Read more >
How we validate input data using pydantic
We use the Python package pydantic for fast and easy validation of ... The second argument is the value to validate, and can...
Read more >
Many-To-Many Relationships In FastAPI - GormAnalysis
This time we set up three models that mimic our database models. So, how does a pydantic model get serialized to JSON? Well,...
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