Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions docs/en/docs/advanced/dataclasses.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ FastAPI is built on top of **Pydantic**, and I have been showing you how to use

But FastAPI also supports using <a href="https://docs.python.org/3/library/dataclasses.html" class="external-link" target="_blank">`dataclasses`</a> the same way:

```Python hl_lines="1 7-12 19-20"
{!../../docs_src/dataclasses/tutorial001.py!}
```
{* ../../docs_src/dataclasses/tutorial001.py hl[1,7:12,19:20] *}

This is still supported thanks to **Pydantic**, as it has <a href="https://docs.pydantic.dev/latest/concepts/dataclasses/#use-of-stdlib-dataclasses-with-basemodel" class="external-link" target="_blank">internal support for `dataclasses`</a>.

Expand Down Expand Up @@ -34,9 +32,7 @@ But if you have a bunch of dataclasses laying around, this is a nice trick to us

You can also use `dataclasses` in the `response_model` parameter:

```Python hl_lines="1 7-13 19"
{!../../docs_src/dataclasses/tutorial002.py!}
```
{* ../../docs_src/dataclasses/tutorial002.py hl[1,7:13,19] *}

The dataclass will be automatically converted to a Pydantic dataclass.

Expand All @@ -52,9 +48,7 @@ In some cases, you might still have to use Pydantic's version of `dataclasses`.

In that case, you can simply swap the standard `dataclasses` with `pydantic.dataclasses`, which is a drop-in replacement:

```{ .python .annotate hl_lines="1 5 8-11 14-17 23-25 28" }
{!../../docs_src/dataclasses/tutorial003.py!}
```
{* ../../docs_src/dataclasses/tutorial003.py hl[1,5,8:11,14:17,23:25,28] *}

1. We still import `field` from standard `dataclasses`.

Expand Down