mirror of
https://github.com/fastapi/fastapi.git
synced 2026-04-27 10:18:11 -04:00
📝 Tweak docs and translations links, typos, format (#11389)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
@@ -136,7 +136,7 @@ We will now use a simple dependency to read a custom `X-Token` header:
|
||||
!!! tip
|
||||
We are using an invented header to simplify this example.
|
||||
|
||||
But in real cases you will get better results using the integrated [Security utilities](./security/index.md){.internal-link target=_blank}.
|
||||
But in real cases you will get better results using the integrated [Security utilities](security/index.md){.internal-link target=_blank}.
|
||||
|
||||
## Another module with `APIRouter`
|
||||
|
||||
@@ -329,7 +329,7 @@ The section:
|
||||
from .routers import items, users
|
||||
```
|
||||
|
||||
Means:
|
||||
means:
|
||||
|
||||
* Starting in the same package that this module (the file `app/main.py`) lives in (the directory `app/`)...
|
||||
* look for the subpackage `routers` (the directory at `app/routers/`)...
|
||||
@@ -373,7 +373,7 @@ from .routers.items import router
|
||||
from .routers.users import router
|
||||
```
|
||||
|
||||
The `router` from `users` would overwrite the one from `items` and we wouldn't be able to use them at the same time.
|
||||
the `router` from `users` would overwrite the one from `items` and we wouldn't be able to use them at the same time.
|
||||
|
||||
So, to be able to use both of them in the same file, we import the submodules directly:
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ You can also use the <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/
|
||||
|
||||
This means that you can send only the data that you want to update, leaving the rest intact.
|
||||
|
||||
!!! Note
|
||||
!!! note
|
||||
`PATCH` is less commonly used and known than `PUT`.
|
||||
|
||||
And many teams use only `PUT`, even for partial updates.
|
||||
|
||||
@@ -271,6 +271,12 @@ Now you can declare your dependency using this class.
|
||||
|
||||
Notice how we write `CommonQueryParams` twice in the above code:
|
||||
|
||||
=== "Python 3.8+"
|
||||
|
||||
```Python
|
||||
commons: Annotated[CommonQueryParams, Depends(CommonQueryParams)]
|
||||
```
|
||||
|
||||
=== "Python 3.8+ non-Annotated"
|
||||
|
||||
!!! tip
|
||||
@@ -280,12 +286,6 @@ Notice how we write `CommonQueryParams` twice in the above code:
|
||||
commons: CommonQueryParams = Depends(CommonQueryParams)
|
||||
```
|
||||
|
||||
=== "Python 3.8+"
|
||||
|
||||
```Python
|
||||
commons: Annotated[CommonQueryParams, Depends(CommonQueryParams)]
|
||||
```
|
||||
|
||||
The last `CommonQueryParams`, in:
|
||||
|
||||
```Python
|
||||
@@ -334,7 +334,7 @@ You could actually write just:
|
||||
commons = Depends(CommonQueryParams)
|
||||
```
|
||||
|
||||
..as in:
|
||||
...as in:
|
||||
|
||||
=== "Python 3.10+"
|
||||
|
||||
@@ -380,6 +380,12 @@ But declaring the type is encouraged as that way your editor will know what will
|
||||
|
||||
But you see that we are having some code repetition here, writing `CommonQueryParams` twice:
|
||||
|
||||
=== "Python 3.8+"
|
||||
|
||||
```Python
|
||||
commons: Annotated[CommonQueryParams, Depends(CommonQueryParams)]
|
||||
```
|
||||
|
||||
=== "Python 3.8+ non-Annotated"
|
||||
|
||||
!!! tip
|
||||
@@ -389,12 +395,6 @@ But you see that we are having some code repetition here, writing `CommonQueryPa
|
||||
commons: CommonQueryParams = Depends(CommonQueryParams)
|
||||
```
|
||||
|
||||
=== "Python 3.8+"
|
||||
|
||||
```Python
|
||||
commons: Annotated[CommonQueryParams, Depends(CommonQueryParams)]
|
||||
```
|
||||
|
||||
**FastAPI** provides a shortcut for these cases, in where the dependency is *specifically* a class that **FastAPI** will "call" to create an instance of the class itself.
|
||||
|
||||
For those specific cases, you can do the following:
|
||||
|
||||
@@ -257,7 +257,7 @@ And you can declare dependencies with `async def` inside of normal `def` *path o
|
||||
It doesn't matter. **FastAPI** will know what to do.
|
||||
|
||||
!!! note
|
||||
If you don't know, check the [Async: *"In a hurry?"*](../../async.md){.internal-link target=_blank} section about `async` and `await` in the docs.
|
||||
If you don't know, check the [Async: *"In a hurry?"*](../../async.md#in-a-hurry){.internal-link target=_blank} section about `async` and `await` in the docs.
|
||||
|
||||
## Integrated with OpenAPI
|
||||
|
||||
|
||||
@@ -83,7 +83,7 @@ So, continuing with the `user_dict` from above, writing:
|
||||
UserInDB(**user_dict)
|
||||
```
|
||||
|
||||
Would result in something equivalent to:
|
||||
would result in something equivalent to:
|
||||
|
||||
```Python
|
||||
UserInDB(
|
||||
|
||||
@@ -338,7 +338,7 @@ Your response model could have default values, like:
|
||||
|
||||
* `description: Union[str, None] = None` (or `str | None = None` in Python 3.10) has a default of `None`.
|
||||
* `tax: float = 10.5` has a default of `10.5`.
|
||||
* `tags: List[str] = []` as a default of an empty list: `[]`.
|
||||
* `tags: List[str] = []` has a default of an empty list: `[]`.
|
||||
|
||||
but you might want to omit them from the result if they were not actually stored.
|
||||
|
||||
|
||||
@@ -305,7 +305,7 @@ This new `examples` field in JSON Schema is **just a `list`** of examples, not a
|
||||
|
||||
### Pydantic and FastAPI `examples`
|
||||
|
||||
When you add `examples` inside of a Pydantic model, using `schema_extra` or `Field(examples=["something"])` that example is added to the **JSON Schema** for that Pydantic model.
|
||||
When you add `examples` inside a Pydantic model, using `schema_extra` or `Field(examples=["something"])` that example is added to the **JSON Schema** for that Pydantic model.
|
||||
|
||||
And that **JSON Schema** of the Pydantic model is included in the **OpenAPI** of your API, and then it's used in the docs UI.
|
||||
|
||||
|
||||
@@ -260,7 +260,7 @@ If the token is invalid, return an HTTP error right away.
|
||||
|
||||
Create a `timedelta` with the expiration time of the token.
|
||||
|
||||
Create a real JWT access token and return it
|
||||
Create a real JWT access token and return it.
|
||||
|
||||
=== "Python 3.10+"
|
||||
|
||||
|
||||
@@ -118,7 +118,7 @@ First, import `OAuth2PasswordRequestForm`, and use it as a dependency with `Depe
|
||||
|
||||
Now, get the user data from the (fake) database, using the `username` from the form field.
|
||||
|
||||
If there is no such user, we return an error saying "incorrect username or password".
|
||||
If there is no such user, we return an error saying "Incorrect username or password".
|
||||
|
||||
For the error, we use the exception `HTTPException`:
|
||||
|
||||
@@ -416,7 +416,7 @@ Password: `secret2`
|
||||
|
||||
And try to use the operation `GET` with the path `/users/me`.
|
||||
|
||||
You will get an "inactive user" error, like:
|
||||
You will get an "Inactive user" error, like:
|
||||
|
||||
```JSON
|
||||
{
|
||||
|
||||
@@ -546,7 +546,7 @@ Our dependency will create a new SQLAlchemy `SessionLocal` that will be used in
|
||||
|
||||
This way we make sure the database session is always closed after the request. Even if there was an exception while processing the request.
|
||||
|
||||
But you can't raise another exception from the exit code (after `yield`). See more in [Dependencies with `yield` and `HTTPException`](./dependencies/dependencies-with-yield.md#dependencies-with-yield-and-httpexception){.internal-link target=_blank}
|
||||
But you can't raise another exception from the exit code (after `yield`). See more in [Dependencies with `yield` and `HTTPException`](dependencies/dependencies-with-yield.md#dependencies-with-yield-and-httpexception){.internal-link target=_blank}
|
||||
|
||||
And then, when using the dependency in a *path operation function*, we declare it with the type `Session` we imported directly from SQLAlchemy.
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ And your **FastAPI** application might also be composed of several files/modules
|
||||
|
||||
### **FastAPI** app file
|
||||
|
||||
Let's say you have a file structure as described in [Bigger Applications](./bigger-applications.md){.internal-link target=_blank}:
|
||||
Let's say you have a file structure as described in [Bigger Applications](bigger-applications.md){.internal-link target=_blank}:
|
||||
|
||||
```
|
||||
.
|
||||
|
||||
Reference in New Issue
Block a user