From cad08bbc4d82450fd7730fe4252512c621457065 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 15 Apr 2025 02:47:42 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20format?= =?UTF-8?q?=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/en/docs/tutorial/request-form-models.md | 8 ++++---- docs_src/request_form_models/tutorial003.py | 8 ++++++-- .../request_form_models/tutorial003_an_py39.py | 10 +++++++--- docs_src/request_form_models/tutorial004.py | 17 ++++++++++------- .../request_form_models/tutorial004_an_py39.py | 17 ++++++++++------- docs_src/request_form_models/tutorial004_pv1.py | 13 ++++++++----- .../tutorial004_pv1_an_py39.py | 13 ++++++++----- fastapi/_compat.py | 4 +++- 8 files changed, 56 insertions(+), 34 deletions(-) diff --git a/docs/en/docs/tutorial/request-form-models.md b/docs/en/docs/tutorial/request-form-models.md index 00ab1c341..885dfc96e 100644 --- a/docs/en/docs/tutorial/request-form-models.md +++ b/docs/en/docs/tutorial/request-form-models.md @@ -97,7 +97,7 @@ and the JSON response is also correct: ``` When the checkbox is *unchecked*, though, something strange happens. -The submitted form data is *empty*, +The submitted form data is *empty*, and the returned JSON data still shows `checkbox` still being `true`! This is because checkboxes in HTML forms don't work exactly like the boolean inputs we expect, @@ -108,7 +108,7 @@ When dealing with form models with defaults, we need to take special care to handle cases where the field being *unset* has a specific meaning. In some cases, we can resolve the problem by changing or removing the default, -but we don't always have that option - +but we don't always have that option - particularly when the model is used in other places than the form (model reuse is one of the benefits of building FastAPI on top of pydantic, after all!). @@ -116,8 +116,8 @@ To do this, you can use a [`model_validator`](https://docs.pydantic.dev/latest/c in the `before` mode - before the defaults from the model are applied, to differentiate between an explicit `False` value and an unset value. -We also don't want to just treat any time the value is unset as ``False`` - -that would defeat the purpose of the default! +We also don't want to just treat any time the value is unset as ``False`` - +that would defeat the purpose of the default! We want to specifically correct the behavior when it is used in the context of a *form.* So we can additionally use the `'fastapi_field'` passed to the diff --git a/docs_src/request_form_models/tutorial003.py b/docs_src/request_form_models/tutorial003.py index 5c2b3dd31..b0cbd6ff3 100644 --- a/docs_src/request_form_models/tutorial003.py +++ b/docs_src/request_form_models/tutorial003.py @@ -1,12 +1,14 @@ -from pydantic import BaseModel from fastapi import FastAPI, Form, Request from fastapi.responses import HTMLResponse from fastapi.templating import Jinja2Templates from jinja2 import DictLoader, Environment +from pydantic import BaseModel + class MyModel(BaseModel): checkbox: bool = True + form_template = """