Browse Source

Merge 56cc87e2ee into e26eb871f8

pull/14653/merge
ynehra0777 4 months ago
committed by GitHub
parent
commit
d3fa58cba9
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 24
      docs/en/docs/tutorial/body.md

24
docs/en/docs/tutorial/body.md

@ -54,6 +54,29 @@ For example, this model above declares a JSON "`object`" (or Python `dict`) like
"price": 45.2
}
```
### Required fields that can be `None`
In Python type hints, a parameter can be **required** and still allow the value
`None`.
This typically happens when you use `Optional[T]` **without** providing a default
value.
For example:
```python
from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
description: Optional[str]
@app.post("/items/")
async def create_item(item: Item):
return item
## Declare it as a parameter { #declare-it-as-a-parameter }
@ -164,3 +187,4 @@ But adding the type annotations will allow your editor to give you better suppor
## Without Pydantic { #without-pydantic }
If you don't want to use Pydantic models, you can also use **Body** parameters. See the docs for [Body - Multiple Parameters: Singular values in body](body-multiple-params.md#singular-values-in-body){.internal-link target=_blank}.

Loading…
Cancel
Save