mirror of
https://github.com/fastapi/fastapi.git
synced 2026-04-15 20:41:36 -04:00
✅ Add docs, tests and fixes for extra data types
including refactor of jsonable_encoder to allow other object and model types
This commit is contained in:
@@ -116,7 +116,7 @@ Again, doing just that declaration, with **FastAPI** you get:
|
||||
|
||||
Apart from normal singular types like `str`, `int`, `float`, etc. You can use more complex singular types that inherit from `str`.
|
||||
|
||||
To see all the options you have, checkout the docs for <a href="https://pydantic-docs.helpmanual.io/#exotic-types" target="_blank">Pydantic's exotic types</a>.
|
||||
To see all the options you have, checkout the docs for <a href="https://pydantic-docs.helpmanual.io/#exotic-types" target="_blank">Pydantic's exotic types</a>. You will see some examples in the next chapter.
|
||||
|
||||
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`:
|
||||
|
||||
|
||||
64
docs/tutorial/extra-data-types.md
Normal file
64
docs/tutorial/extra-data-types.md
Normal file
@@ -0,0 +1,64 @@
|
||||
Up to now, you have been using common data types, like:
|
||||
|
||||
* `int`
|
||||
* `float`
|
||||
* `str`
|
||||
* `bool`
|
||||
|
||||
But you can also use more complex data types.
|
||||
|
||||
And you will still have the same features as seen up to now:
|
||||
|
||||
* Great editor support.
|
||||
* Data conversion from incoming requests.
|
||||
* Data conversion for response data.
|
||||
* Data validation.
|
||||
* Automatic annotation and documentation.
|
||||
|
||||
## Other data types
|
||||
|
||||
Here are some of the additional data types you can use:
|
||||
|
||||
* `UUID`:
|
||||
* A standard "Universally Unique Identifier", common as an ID in many databases and systems.
|
||||
* In requests and responses will be represented as a `str`.
|
||||
* `datetime.datetime`:
|
||||
* A Python `datetime.datetime`.
|
||||
* In requests and responses will be represented as a `str` in ISO 8601 format, like: `2008-09-15T15:53:00+05:00`.
|
||||
* `datetime.date`:
|
||||
* Python `datetime.date`.
|
||||
* In requests and responses will be represented as a `str` in ISO 8601 format, like: `2008-09-15`.
|
||||
* `datetime.time`:
|
||||
* A Python `datetime.time`.
|
||||
* In requests and responses will be represented as a `str` in ISO 8601 format, like: `14:23:55.003`.
|
||||
* `datetime.timedelta`:
|
||||
* A Python `datetime.timedelta`.
|
||||
* In requests and responses will be represented as a `float` of total seconds.
|
||||
* Pydantic also allows representing it as a "ISO 8601 time diff encoding", <a href="https://pydantic-docs.helpmanual.io/#json-serialisation" target="_blank">see the docs for more info</a>.
|
||||
* `frozenset`:
|
||||
* In requests and responses, treated the same as a `set`:
|
||||
* In requests, a list will be read, eliminating duplicates and converting it to a `set`.
|
||||
* In responses, the `set` will be converted to a `list`.
|
||||
* The generated schema will specify that the `set` values are unique (using JSON Schema's `uniqueItems`).
|
||||
* `bytes`:
|
||||
* Standard Python `bytes`.
|
||||
* In requests and responses will be treated as `str`.
|
||||
* The generated schema will specify that it's a `str` with `binary` "format".
|
||||
* `Decimal`:
|
||||
* Standard Python `Decimal`.
|
||||
* In requests and responses, handled the same as a `float`.
|
||||
|
||||
|
||||
## Example
|
||||
|
||||
Here's an example path operation with parameters using some of the above types.
|
||||
|
||||
```Python hl_lines="1 2 11 12 13 14 15"
|
||||
{!./src/extra_data_types/tutorial001.py!}
|
||||
```
|
||||
|
||||
Note that the parameters inside the function have their natural data type, and you can, for example, perform normal date manipulations, like:
|
||||
|
||||
```Python hl_lines="17 18"
|
||||
{!./src/extra_data_types/tutorial001.py!}
|
||||
```
|
||||
@@ -25,7 +25,7 @@ In this case, `item_id` is declared to be an `int`.
|
||||
!!! check
|
||||
This will give you editor support inside of your function, with error checks, completion, etc.
|
||||
|
||||
## Data "parsing"
|
||||
## Data <abbr title="also known as: serialization, parsing, marshalling">conversion</abbr>
|
||||
|
||||
If you run this example and open your browser at <a href="http://127.0.0.1:8000/items/3" target="_blank">http://127.0.0.1:8000/items/3</a>, you will see a response of:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user