Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

教程-用户指南 中的 请求体-多个参数 章节中代码错误 #4976

Closed
9 tasks done
liufei1997 opened this issue Jun 2, 2022 · 1 comment
Closed
9 tasks done
Labels
question Question or problem question-migrate

Comments

@liufei1997
Copy link

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 Union

from fastapi import Body, FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None


class User(BaseModel):
    username: str
    full_name: Union[str, None] = None


@app.put("/items/{item_id}")
async def update_item(
    *,
    item_id: int,
    item: Item,
    user: User,
    importance: int = Body(gt=0),
    q: Union[str, None] = None
):
    results = {"item_id": item_id, "item": item, "user": user, "importance": importance}
    if q:
        results.update({"q": q})
    return results

Description

教程-用户指南 中的 请求体-多个参数 中的 多个请求体参数和查询参数 中的示例代码出现错误
https://fastapi.tiangolo.com/zh/tutorial/body-multiple-params/#_3
image

Body参数缺少一个位置参数,即TypeError: Body() missing 1 required positional argument: 'default'

个人建议改为 Body(..., gt=0),

Operating System

Windows

Operating System Details

No response

FastAPI Version

Python Version

3.7

Additional Context

No response

@liufei1997 liufei1997 added the question Question or problem label Jun 2, 2022
@unights
Copy link

unights commented Jun 6, 2022

Hi @liufei1997
Maybe you need to update the version of fastapi to 0.78.0.
See #4911 (comment)

Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #8511 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

No branches or pull requests

3 participants