mirror of https://github.com/tiangolo/fastapi.git
📝 Update docs highlighting after re-sort imports
coming back to flit, after pipenv
This commit is contained in:
parent
7da9625505
commit
e6ca71523b
|
|
@ -6,7 +6,7 @@ First, of course, you can mix `Path`, `Query` and request body parameter declara
|
||||||
|
|
||||||
And you can also declare body parameters as optional, by setting the default to `None`:
|
And you can also declare body parameters as optional, by setting the default to `None`:
|
||||||
|
|
||||||
```Python hl_lines="18 19 20"
|
```Python hl_lines="17 18 19"
|
||||||
{!./tutorial/src/body_multiple_params/tutorial001.py!}
|
{!./tutorial/src/body_multiple_params/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -29,7 +29,7 @@ In the previous example, the path operations would expect a JSON body with the a
|
||||||
|
|
||||||
But you can also declare multiple body parameters, e.g. `item` and `user`:
|
But you can also declare multiple body parameters, e.g. `item` and `user`:
|
||||||
|
|
||||||
```Python hl_lines="21"
|
```Python hl_lines="20"
|
||||||
{!./tutorial/src/body_multiple_params/tutorial002.py!}
|
{!./tutorial/src/body_multiple_params/tutorial002.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -71,7 +71,7 @@ If you declare it as is, because it is a singular value, **FastAPI** will assume
|
||||||
But you can instruct **FastAPI** to treat it as another body key using `Body`:
|
But you can instruct **FastAPI** to treat it as another body key using `Body`:
|
||||||
|
|
||||||
|
|
||||||
```Python hl_lines="22"
|
```Python hl_lines="21"
|
||||||
{!./tutorial/src/body_multiple_params/tutorial003.py!}
|
{!./tutorial/src/body_multiple_params/tutorial003.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -108,7 +108,7 @@ q: str = None
|
||||||
|
|
||||||
as in:
|
as in:
|
||||||
|
|
||||||
```Python hl_lines="27"
|
```Python hl_lines="25"
|
||||||
{!./tutorial/src/body_multiple_params/tutorial004.py!}
|
{!./tutorial/src/body_multiple_params/tutorial004.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -130,7 +130,7 @@ item: Item = Body(..., embed=True)
|
||||||
|
|
||||||
as in:
|
as in:
|
||||||
|
|
||||||
```Python hl_lines="16"
|
```Python hl_lines="15"
|
||||||
{!./tutorial/src/body_multiple_params/tutorial005.py!}
|
{!./tutorial/src/body_multiple_params/tutorial005.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ With **FastAPI**, you can define, validate, document, and use arbitrarily deeply
|
||||||
|
|
||||||
You can define an attribute to be a subtype. For example, a Python `list`:
|
You can define an attribute to be a subtype. For example, a Python `list`:
|
||||||
|
|
||||||
```Python hl_lines="13"
|
```Python hl_lines="12"
|
||||||
{!./tutorial/src/body_nested_models/tutorial001.py!}
|
{!./tutorial/src/body_nested_models/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -41,7 +41,7 @@ Use that same standard syntax for model attributes with subtypes.
|
||||||
|
|
||||||
So, in our example, we can make `tags` be specifically a "list of strings":
|
So, in our example, we can make `tags` be specifically a "list of strings":
|
||||||
|
|
||||||
```Python hl_lines="15"
|
```Python hl_lines="14"
|
||||||
{!./tutorial/src/body_nested_models/tutorial002.py!}
|
{!./tutorial/src/body_nested_models/tutorial002.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -53,7 +53,7 @@ And Python has a special data type for sets of unique items, the `set`.
|
||||||
|
|
||||||
Then we can import `Set` and declare `tags` as a `set` of `str`:
|
Then we can import `Set` and declare `tags` as a `set` of `str`:
|
||||||
|
|
||||||
```Python hl_lines="1 15"
|
```Python hl_lines="1 14"
|
||||||
{!./tutorial/src/body_nested_models/tutorial003.py!}
|
{!./tutorial/src/body_nested_models/tutorial003.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -77,7 +77,7 @@ All that, arbitrarily nested.
|
||||||
|
|
||||||
For example, we can define an `Image` model:
|
For example, we can define an `Image` model:
|
||||||
|
|
||||||
```Python hl_lines="10 11 12"
|
```Python hl_lines="9 10 11"
|
||||||
{!./tutorial/src/body_nested_models/tutorial004.py!}
|
{!./tutorial/src/body_nested_models/tutorial004.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -85,7 +85,7 @@ For example, we can define an `Image` model:
|
||||||
|
|
||||||
And then we can use it as the type of an attribute:
|
And then we can use it as the type of an attribute:
|
||||||
|
|
||||||
```Python hl_lines="21"
|
```Python hl_lines="20"
|
||||||
{!./tutorial/src/body_nested_models/tutorial004.py!}
|
{!./tutorial/src/body_nested_models/tutorial004.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -120,7 +120,7 @@ To see all the options you have, checkout the docs for <a href="https://pydantic
|
||||||
|
|
||||||
For example, as in the `Image` model we have a `url` field, we can declare it to be instead of a `str`, a Pydantic's `UrlStr`:
|
For example, as in the `Image` model we have a `url` field, we can declare it to be instead of a `str`, a Pydantic's `UrlStr`:
|
||||||
|
|
||||||
```Python hl_lines="4 12"
|
```Python hl_lines="5 11"
|
||||||
{!./tutorial/src/body_nested_models/tutorial005.py!}
|
{!./tutorial/src/body_nested_models/tutorial005.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -130,7 +130,7 @@ The string will be checked to be a valid URL, and documented in JSON Schema / Op
|
||||||
|
|
||||||
You can also use Pydantic models as subtypes of `list`, `set`, etc:
|
You can also use Pydantic models as subtypes of `list`, `set`, etc:
|
||||||
|
|
||||||
```Python hl_lines="22"
|
```Python hl_lines="21"
|
||||||
{!./tutorial/src/body_nested_models/tutorial006.py!}
|
{!./tutorial/src/body_nested_models/tutorial006.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -167,7 +167,7 @@ This will expect (convert, validate, document, etc) a JSON body like:
|
||||||
|
|
||||||
You can define arbitrarily deeply nested models:
|
You can define arbitrarily deeply nested models:
|
||||||
|
|
||||||
```Python hl_lines="11 16 22 25 29"
|
```Python hl_lines="10 15 21 24 28"
|
||||||
{!./tutorial/src/body_nested_models/tutorial007.py!}
|
{!./tutorial/src/body_nested_models/tutorial007.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -184,7 +184,7 @@ images: List[Image]
|
||||||
|
|
||||||
as in:
|
as in:
|
||||||
|
|
||||||
```Python hl_lines="17"
|
```Python hl_lines="16"
|
||||||
{!./tutorial/src/body_nested_models/tutorial008.py!}
|
{!./tutorial/src/body_nested_models/tutorial008.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ The same way you can declare additional validation and metadata in path operatio
|
||||||
|
|
||||||
First, you have to import it:
|
First, you have to import it:
|
||||||
|
|
||||||
```Python hl_lines="3"
|
```Python hl_lines="2"
|
||||||
{!./tutorial/src/body_schema/tutorial001.py!}
|
{!./tutorial/src/body_schema/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -16,7 +16,7 @@ First, you have to import it:
|
||||||
|
|
||||||
You can then use `Schema` with model attributes:
|
You can then use `Schema` with model attributes:
|
||||||
|
|
||||||
```Python hl_lines="10 11"
|
```Python hl_lines="9 10"
|
||||||
{!./tutorial/src/body_schema/tutorial001.py!}
|
{!./tutorial/src/body_schema/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -44,7 +44,7 @@ If you know JSON Schema and want to add extra information appart from what we ha
|
||||||
|
|
||||||
For example, you can use that functionality to pass a <a href="http://json-schema.org/latest/json-schema-validation.html#rfc.section.8.5" target="_blank">JSON Schema example</a> field to a body request JSON Schema:
|
For example, you can use that functionality to pass a <a href="http://json-schema.org/latest/json-schema-validation.html#rfc.section.8.5" target="_blank">JSON Schema example</a> field to a body request JSON Schema:
|
||||||
|
|
||||||
```Python hl_lines="21 22 23 24 25 26"
|
```Python hl_lines="20 21 22 23 24 25"
|
||||||
{!./tutorial/src/body_schema/tutorial002.py!}
|
{!./tutorial/src/body_schema/tutorial002.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ To declare a request body, you use <a href="https://pydantic-docs.helpmanual.io/
|
||||||
|
|
||||||
First, you need to import `BaseModel` from `pydantic`:
|
First, you need to import `BaseModel` from `pydantic`:
|
||||||
|
|
||||||
```Python hl_lines="1"
|
```Python hl_lines="2"
|
||||||
{!./tutorial/src/body/tutorial001.py!}
|
{!./tutorial/src/body/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -14,7 +14,7 @@ Then you declare your data model as a class that inherits from `BaseModel`.
|
||||||
|
|
||||||
Use standard Python types for all the attributes:
|
Use standard Python types for all the attributes:
|
||||||
|
|
||||||
```Python hl_lines="6 7 8 9 10"
|
```Python hl_lines="5 6 7 8 9"
|
||||||
{!./tutorial/src/body/tutorial001.py!}
|
{!./tutorial/src/body/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -44,7 +44,7 @@ For example, this model above declares a JSON "`object`" (or Python `dict`) like
|
||||||
|
|
||||||
To add it to your path operation, declare it the same way you declared path and query parameters:
|
To add it to your path operation, declare it the same way you declared path and query parameters:
|
||||||
|
|
||||||
```Python hl_lines="17"
|
```Python hl_lines="16"
|
||||||
{!./tutorial/src/body/tutorial001.py!}
|
{!./tutorial/src/body/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -100,7 +100,7 @@ But you would get the same editor support with <a href="https://www.jetbrains.co
|
||||||
|
|
||||||
Inside of the function, you can access all the attributes of the model object directly:
|
Inside of the function, you can access all the attributes of the model object directly:
|
||||||
|
|
||||||
```Python hl_lines="20"
|
```Python hl_lines="19"
|
||||||
{!./tutorial/src/body/tutorial002.py!}
|
{!./tutorial/src/body/tutorial002.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -110,7 +110,7 @@ You can declare path parameters and body requests at the same time.
|
||||||
|
|
||||||
**FastAPI** will recognize that the function parameters that match path parameters should be **taken from the path**, and that function parameters that are declared to be Pydantic models should be **taken from the request body**.
|
**FastAPI** will recognize that the function parameters that match path parameters should be **taken from the path**, and that function parameters that are declared to be Pydantic models should be **taken from the request body**.
|
||||||
|
|
||||||
```Python hl_lines="16 17"
|
```Python hl_lines="15 16"
|
||||||
{!./tutorial/src/body/tutorial003.py!}
|
{!./tutorial/src/body/tutorial003.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -120,7 +120,7 @@ You can also declare **body**, **path** and **query** parameters, all at the sam
|
||||||
|
|
||||||
**FastAPI** will recognize each of them and take the data from the correct place.
|
**FastAPI** will recognize each of them and take the data from the correct place.
|
||||||
|
|
||||||
```Python hl_lines="17"
|
```Python hl_lines="16"
|
||||||
{!./tutorial/src/body/tutorial004.py!}
|
{!./tutorial/src/body/tutorial004.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ For example, if you are squeezing performance, you can use `ujson` and set the r
|
||||||
|
|
||||||
### Import `UJSONResponse`
|
### Import `UJSONResponse`
|
||||||
|
|
||||||
```Python hl_lines="1"
|
```Python hl_lines="2"
|
||||||
{!./tutorial/src/custom_response/tutorial001.py!}
|
{!./tutorial/src/custom_response/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -24,7 +24,7 @@ For example, if you are squeezing performance, you can use `ujson` and set the r
|
||||||
|
|
||||||
Make your path operation use `UJSONResponse` as the response class using the parameter `content_type`:
|
Make your path operation use `UJSONResponse` as the response class using the parameter `content_type`:
|
||||||
|
|
||||||
```Python hl_lines="8"
|
```Python hl_lines="7"
|
||||||
{!./tutorial/src/custom_response/tutorial001.py!}
|
{!./tutorial/src/custom_response/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -39,7 +39,7 @@ To return a response with HTML directly from **FastAPI**, use `HTMLResponse`.
|
||||||
|
|
||||||
### Import `HTMLResponse`
|
### Import `HTMLResponse`
|
||||||
|
|
||||||
```Python hl_lines="1"
|
```Python hl_lines="2"
|
||||||
{!./tutorial/src/custom_response/tutorial002.py!}
|
{!./tutorial/src/custom_response/tutorial002.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -51,7 +51,7 @@ To return a response with HTML directly from **FastAPI**, use `HTMLResponse`.
|
||||||
|
|
||||||
Pass `HTMLResponse` as the parameter `content_type` of your path operation:
|
Pass `HTMLResponse` as the parameter `content_type` of your path operation:
|
||||||
|
|
||||||
```Python hl_lines="8"
|
```Python hl_lines="7"
|
||||||
{!./tutorial/src/custom_response/tutorial002.py!}
|
{!./tutorial/src/custom_response/tutorial002.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -71,7 +71,7 @@ If you return an object that is an instance of Starlette's `Response`, it will b
|
||||||
|
|
||||||
The same example from above, returning an `HTMLResponse`, could look like:
|
The same example from above, returning an `HTMLResponse`, could look like:
|
||||||
|
|
||||||
```Python hl_lines="8 20"
|
```Python hl_lines="7"
|
||||||
{!./tutorial/src/custom_response/tutorial003.py!}
|
{!./tutorial/src/custom_response/tutorial003.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -92,7 +92,7 @@ The `content_type` class will then be used only to document the OpenAPI path ope
|
||||||
|
|
||||||
For example, it could be something like:
|
For example, it could be something like:
|
||||||
|
|
||||||
```Python hl_lines="8 19 22"
|
```Python hl_lines="7 23"
|
||||||
{!./tutorial/src/custom_response/tutorial004.py!}
|
{!./tutorial/src/custom_response/tutorial004.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -104,7 +104,7 @@ By returning the result of calling `generate_html_response()`, you are already r
|
||||||
|
|
||||||
But by declaring it also in the path operation decorator:
|
But by declaring it also in the path operation decorator:
|
||||||
|
|
||||||
```Python hl_lines="22"
|
```Python hl_lines="21"
|
||||||
{!./tutorial/src/custom_response/tutorial004.py!}
|
{!./tutorial/src/custom_response/tutorial004.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ This is especially the case for user models, because:
|
||||||
|
|
||||||
Here's a general idea of how the models could look like with their password fields and the places where they are used:
|
Here's a general idea of how the models could look like with their password fields and the places where they are used:
|
||||||
|
|
||||||
```Python hl_lines="9 11 16 22 24 33 35 40 41"
|
```Python hl_lines="8 10 15 21 23 32 34 39 40"
|
||||||
{!./tutorial/src/extra_models/tutorial001.py!}
|
{!./tutorial/src/extra_models/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -36,7 +36,7 @@ All the data conversion, validation, documentation, etc. will still work as norm
|
||||||
|
|
||||||
That way, we can declare just the differences between the models (with plaintext `password`, with `hashed_password` and without password):
|
That way, we can declare just the differences between the models (with plaintext `password`, with `hashed_password` and without password):
|
||||||
|
|
||||||
```Python hl_lines="9 15 16 19 20 23 24"
|
```Python hl_lines="8 14 15 18 19 22 23"
|
||||||
{!./tutorial/src/extra_models/tutorial002.py!}
|
{!./tutorial/src/extra_models/tutorial002.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ You can adapt it to any other NoSQL database like:
|
||||||
|
|
||||||
For now, don't pay attention to the rest, only the imports:
|
For now, don't pay attention to the rest, only the imports:
|
||||||
|
|
||||||
```Python hl_lines="5 6 7"
|
```Python hl_lines="6 7 8"
|
||||||
{!./tutorial/src/nosql_databases/tutorial001.py!}
|
{!./tutorial/src/nosql_databases/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -49,7 +49,7 @@ This utility function will:
|
||||||
* Set defaults for timeouts.
|
* Set defaults for timeouts.
|
||||||
* Return it.
|
* Return it.
|
||||||
|
|
||||||
```Python hl_lines="13 14 15 16 17 18 19 20 21 22"
|
```Python hl_lines="13 14 15 16 17 18 19 20"
|
||||||
{!./tutorial/src/nosql_databases/tutorial001.py!}
|
{!./tutorial/src/nosql_databases/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -61,7 +61,7 @@ As **Couchbase** "documents" are actually just "JSON objects", we can model them
|
||||||
|
|
||||||
First, let's create a `User` model:
|
First, let's create a `User` model:
|
||||||
|
|
||||||
```Python hl_lines="25 26 27 28 29"
|
```Python hl_lines="23 24 25 26 27"
|
||||||
{!./tutorial/src/nosql_databases/tutorial001.py!}
|
{!./tutorial/src/nosql_databases/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -75,7 +75,7 @@ This will have the data that is actually stored in the database.
|
||||||
|
|
||||||
We don't create it as a subclass of Pydantic's `BaseModel` but as a subclass of our own `User`, because it will have all the attributes in `User` plus a couple more:
|
We don't create it as a subclass of Pydantic's `BaseModel` but as a subclass of our own `User`, because it will have all the attributes in `User` plus a couple more:
|
||||||
|
|
||||||
```Python hl_lines="32 33 34"
|
```Python hl_lines="30 31 32"
|
||||||
{!./tutorial/src/nosql_databases/tutorial001.py!}
|
{!./tutorial/src/nosql_databases/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -96,7 +96,7 @@ Now create a function that will:
|
||||||
|
|
||||||
By creating a function that is only dedicated to getting your user from a `username` (or any other parameter) independent of your path operation function, you can more easily re-use it in multiple parts and also add <abbr title="Automated test, written in code, that checks if another piece of code is working correctly.">unit tests</abbr> for it:
|
By creating a function that is only dedicated to getting your user from a `username` (or any other parameter) independent of your path operation function, you can more easily re-use it in multiple parts and also add <abbr title="Automated test, written in code, that checks if another piece of code is working correctly.">unit tests</abbr> for it:
|
||||||
|
|
||||||
```Python hl_lines="37 38 39 40 41 42 43"
|
```Python hl_lines="35 36 37 38 39 40 41"
|
||||||
{!./tutorial/src/nosql_databases/tutorial001.py!}
|
{!./tutorial/src/nosql_databases/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -131,7 +131,7 @@ UserInDB(username="johndoe", hashed_password="some_hash")
|
||||||
|
|
||||||
### Create the `FastAPI` app
|
### Create the `FastAPI` app
|
||||||
|
|
||||||
```Python hl_lines="47"
|
```Python hl_lines="45"
|
||||||
{!./tutorial/src/nosql_databases/tutorial001.py!}
|
{!./tutorial/src/nosql_databases/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -141,7 +141,7 @@ As our code is calling Couchbase and we are not using the <a href="https://docs.
|
||||||
|
|
||||||
Also, Couchbase recommends not using a single `Bucket` object in multiple "<abbr title="A sequence of code being executed by the program, while at the same time, or at intervals, there can be others being executed too.">thread</abbr>s", so, we can get just get the bucket directly and pass it to our utility functions:
|
Also, Couchbase recommends not using a single `Bucket` object in multiple "<abbr title="A sequence of code being executed by the program, while at the same time, or at intervals, there can be others being executed too.">thread</abbr>s", so, we can get just get the bucket directly and pass it to our utility functions:
|
||||||
|
|
||||||
```Python hl_lines="50 51 52 53 54"
|
```Python hl_lines="48 49 50 51 52"
|
||||||
{!./tutorial/src/nosql_databases/tutorial001.py!}
|
{!./tutorial/src/nosql_databases/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ You can pass directly the `int` code, like `404`.
|
||||||
|
|
||||||
But if you don't remember what each number code is for, you can use the shortcut constants from `starlette`:
|
But if you don't remember what each number code is for, you can use the shortcut constants from `starlette`:
|
||||||
|
|
||||||
```Python hl_lines="4 19"
|
```Python hl_lines="5 18"
|
||||||
{!./tutorial/src/path_operation_configuration/tutorial001.py!}
|
{!./tutorial/src/path_operation_configuration/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -22,7 +22,7 @@ That status code will be used in the response and will be added to the OpenAPI s
|
||||||
|
|
||||||
You can add tags to your path operation, pass the parameter `tags` with a `list` of `str` (commonly just one `str`):
|
You can add tags to your path operation, pass the parameter `tags` with a `list` of `str` (commonly just one `str`):
|
||||||
|
|
||||||
```Python hl_lines="18 23 28"
|
```Python hl_lines="17 22 27"
|
||||||
{!./tutorial/src/path_operation_configuration/tutorial002.py!}
|
{!./tutorial/src/path_operation_configuration/tutorial002.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -34,7 +34,7 @@ They will be added to the OpenAPI schema and used by the automatic documentation
|
||||||
|
|
||||||
You can add a `summary` and `description`:
|
You can add a `summary` and `description`:
|
||||||
|
|
||||||
```Python hl_lines="21 22"
|
```Python hl_lines="20 21"
|
||||||
{!./tutorial/src/path_operation_configuration/tutorial003.py!}
|
{!./tutorial/src/path_operation_configuration/tutorial003.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -42,7 +42,7 @@ You can add a `summary` and `description`:
|
||||||
|
|
||||||
As descriptions tend to be long and cover multiple lines, you can declare the path operation description in the function <abbr title="a multi-line string as the first expression inside a function (not assigned to any variable) used for documentation">docstring</abbr> and **FastAPI** will read it from there.
|
As descriptions tend to be long and cover multiple lines, you can declare the path operation description in the function <abbr title="a multi-line string as the first expression inside a function (not assigned to any variable) used for documentation">docstring</abbr> and **FastAPI** will read it from there.
|
||||||
|
|
||||||
```Python hl_lines="20 21 22 23 24 25 26 27 28"
|
```Python hl_lines="19 20 21 22 23 24 25 26 27"
|
||||||
{!./tutorial/src/path_operation_configuration/tutorial004.py!}
|
{!./tutorial/src/path_operation_configuration/tutorial004.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -57,7 +57,7 @@ It will be used in the interactive docs:
|
||||||
|
|
||||||
You can specify the response description with the parameter `response_description`:
|
You can specify the response description with the parameter `response_description`:
|
||||||
|
|
||||||
```Python hl_lines="22"
|
```Python hl_lines="21"
|
||||||
{!./tutorial/src/path_operation_configuration/tutorial005.py!}
|
{!./tutorial/src/path_operation_configuration/tutorial005.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ You can declare the model used for the response with the parameter `response_mod
|
||||||
* `@app.delete()`
|
* `@app.delete()`
|
||||||
* etc.
|
* etc.
|
||||||
|
|
||||||
```Python hl_lines="18"
|
```Python hl_lines="17"
|
||||||
{!./tutorial/src/response_model/tutorial001.py!}
|
{!./tutorial/src/response_model/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -28,13 +28,13 @@ But most importantly:
|
||||||
|
|
||||||
Here we are declaring a `UserIn` model, it will contain a plaintext password:
|
Here we are declaring a `UserIn` model, it will contain a plaintext password:
|
||||||
|
|
||||||
```Python hl_lines="9 11"
|
```Python hl_lines="8 10"
|
||||||
{!./tutorial/src/response_model/tutorial002.py!}
|
{!./tutorial/src/response_model/tutorial002.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
And we are using this model to declare our input and the same model to declare our output:
|
And we are using this model to declare our input and the same model to declare our output:
|
||||||
|
|
||||||
```Python hl_lines="17 18"
|
```Python hl_lines="16 17"
|
||||||
{!./tutorial/src/response_model/tutorial002.py!}
|
{!./tutorial/src/response_model/tutorial002.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -51,19 +51,19 @@ But if we use sthe same model for another path operation, we could be sending th
|
||||||
|
|
||||||
We can instead create an input model with the plaintext password and an output model without it:
|
We can instead create an input model with the plaintext password and an output model without it:
|
||||||
|
|
||||||
```Python hl_lines="9 11 16"
|
```Python hl_lines="8 10 15"
|
||||||
{!./tutorial/src/response_model/tutorial003.py!}
|
{!./tutorial/src/response_model/tutorial003.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
Here, even though our path operation function is returning the same input user that contains the password:
|
Here, even though our path operation function is returning the same input user that contains the password:
|
||||||
|
|
||||||
```Python hl_lines="24"
|
```Python hl_lines="23"
|
||||||
{!./tutorial/src/response_model/tutorial003.py!}
|
{!./tutorial/src/response_model/tutorial003.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
...we declared the `response_model` to be our model `UserOut`, that doesn't include the password:
|
...we declared the `response_model` to be our model `UserOut`, that doesn't include the password:
|
||||||
|
|
||||||
```Python hl_lines="22"
|
```Python hl_lines="21"
|
||||||
{!./tutorial/src/response_model/tutorial003.py!}
|
{!./tutorial/src/response_model/tutorial003.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ In this example, we'll use **PostgreSQL**.
|
||||||
|
|
||||||
For now, don't pay attention to the rest, only the imports:
|
For now, don't pay attention to the rest, only the imports:
|
||||||
|
|
||||||
```Python hl_lines="2 3 4"
|
```Python hl_lines="3 4 5"
|
||||||
{!./tutorial/src/sql_databases/tutorial001.py!}
|
{!./tutorial/src/sql_databases/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -31,7 +31,7 @@ For now, don't pay attention to the rest, only the imports:
|
||||||
|
|
||||||
Define the database that SQLAlchemy should connect to:
|
Define the database that SQLAlchemy should connect to:
|
||||||
|
|
||||||
```Python hl_lines="7"
|
```Python hl_lines="8"
|
||||||
{!./tutorial/src/sql_databases/tutorial001.py!}
|
{!./tutorial/src/sql_databases/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -40,13 +40,13 @@ Define the database that SQLAlchemy should connect to:
|
||||||
|
|
||||||
## Create the SQLAlchemy `engine`
|
## Create the SQLAlchemy `engine`
|
||||||
|
|
||||||
```Python hl_lines="9"
|
```Python hl_lines="10"
|
||||||
{!./tutorial/src/sql_databases/tutorial001.py!}
|
{!./tutorial/src/sql_databases/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Create a `scoped_session`
|
## Create a `scoped_session`
|
||||||
|
|
||||||
```Python hl_lines="10 11 12"
|
```Python hl_lines="11 12 13"
|
||||||
{!./tutorial/src/sql_databases/tutorial001.py!}
|
{!./tutorial/src/sql_databases/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue