18 changed files with 71 additions and 212 deletions
@ -7,7 +7,8 @@ on: |
|||||
- synchronize |
- synchronize |
||||
|
|
||||
env: |
env: |
||||
IS_FORK: ${{ github.event.pull_request.head.repo.full_name != github.repository }} |
# Forks and Dependabot don't have access to secrets |
||||
|
HAS_SECRETS: ${{ secrets.PRE_COMMIT != '' }} |
||||
|
|
||||
jobs: |
jobs: |
||||
pre-commit: |
pre-commit: |
||||
@ -19,7 +20,7 @@ jobs: |
|||||
run: echo "$GITHUB_CONTEXT" |
run: echo "$GITHUB_CONTEXT" |
||||
- uses: actions/checkout@v5 |
- uses: actions/checkout@v5 |
||||
name: Checkout PR for own repo |
name: Checkout PR for own repo |
||||
if: env.IS_FORK == 'false' |
if: env.HAS_SECRETS == 'true' |
||||
with: |
with: |
||||
# To be able to commit it needs to fetch the head of the branch, not the |
# To be able to commit it needs to fetch the head of the branch, not the |
||||
# merge commit |
# merge commit |
||||
@ -31,7 +32,7 @@ jobs: |
|||||
# pre-commit lite ci needs the default checkout configs to work |
# pre-commit lite ci needs the default checkout configs to work |
||||
- uses: actions/checkout@v5 |
- uses: actions/checkout@v5 |
||||
name: Checkout PR for fork |
name: Checkout PR for fork |
||||
if: env.IS_FORK == 'true' |
if: env.HAS_SECRETS == 'false' |
||||
with: |
with: |
||||
# To be able to commit it needs the head branch of the PR, the remote one |
# To be able to commit it needs the head branch of the PR, the remote one |
||||
ref: ${{ github.event.pull_request.head.sha }} |
ref: ${{ github.event.pull_request.head.sha }} |
||||
@ -56,7 +57,7 @@ jobs: |
|||||
run: uvx prek run --from-ref origin/${GITHUB_BASE_REF} --to-ref HEAD --show-diff-on-failure |
run: uvx prek run --from-ref origin/${GITHUB_BASE_REF} --to-ref HEAD --show-diff-on-failure |
||||
continue-on-error: true |
continue-on-error: true |
||||
- name: Commit and push changes |
- name: Commit and push changes |
||||
if: env.IS_FORK == 'false' |
if: env.HAS_SECRETS == 'true' |
||||
run: | |
run: | |
||||
git config user.name "github-actions[bot]" |
git config user.name "github-actions[bot]" |
||||
git config user.email "github-actions[bot]@users.noreply.github.com" |
git config user.email "github-actions[bot]@users.noreply.github.com" |
||||
@ -68,7 +69,7 @@ jobs: |
|||||
git push |
git push |
||||
fi |
fi |
||||
- uses: pre-commit-ci/[email protected] |
- uses: pre-commit-ci/[email protected] |
||||
if: env.IS_FORK == 'true' |
if: env.HAS_SECRETS == 'false' |
||||
with: |
with: |
||||
msg: 🎨 Auto format |
msg: 🎨 Auto format |
||||
- name: Error out on pre-commit errors |
- name: Error out on pre-commit errors |
||||
|
|||||
@ -22,21 +22,13 @@ Hier ist eine allgemeine Idee, wie die Modelle mit ihren Passwortfeldern aussehe |
|||||
|
|
||||
{* ../../docs_src/extra_models/tutorial001_py310.py hl[7,9,14,20,22,27:28,31:33,38:39] *} |
{* ../../docs_src/extra_models/tutorial001_py310.py hl[7,9,14,20,22,27:28,31:33,38:39] *} |
||||
|
|
||||
/// info | Info |
### Über `**user_in.model_dump()` { #about-user-in-model-dump } |
||||
|
|
||||
In Pydantic v1 hieß die Methode `.dict()`, in Pydantic v2 wurde sie <abbr title="veraltet, obsolet: Es soll nicht mehr verwendet werden">deprecatet</abbr> (aber weiterhin unterstützt) und in `.model_dump()` umbenannt. |
#### Pydantics `.model_dump()` { #pydantics-model-dump } |
||||
|
|
||||
Die Beispiele hier verwenden `.dict()` für die Kompatibilität mit Pydantic v1, aber Sie sollten `.model_dump()` verwenden, wenn Sie Pydantic v2 verwenden können. |
|
||||
|
|
||||
/// |
|
||||
|
|
||||
### Über `**user_in.dict()` { #about-user-in-dict } |
|
||||
|
|
||||
#### Die `.dict()`-Methode von Pydantic { #pydantics-dict } |
|
||||
|
|
||||
`user_in` ist ein Pydantic-Modell der Klasse `UserIn`. |
`user_in` ist ein Pydantic-Modell der Klasse `UserIn`. |
||||
|
|
||||
Pydantic-Modelle haben eine `.dict()`-Methode, die ein <abbr title="Dictionary – Zuordnungstabelle: In anderen Sprachen auch Hash, Map, Objekt, Assoziatives Array genannt">`dict`</abbr> mit den Daten des Modells zurückgibt. |
Pydantic-Modelle haben eine `.model_dump()`-Methode, die ein <abbr title="Dictionary – Zuordnungstabelle: In anderen Sprachen auch Hash, Map, Objekt, Assoziatives Array genannt">`dict`</abbr> mit den Daten des Modells zurückgibt. |
||||
|
|
||||
Wenn wir also ein Pydantic-Objekt `user_in` erstellen, etwa so: |
Wenn wir also ein Pydantic-Objekt `user_in` erstellen, etwa so: |
||||
|
|
||||
@ -47,7 +39,7 @@ user_in = UserIn(username="john", password="secret", email="[email protected] |
|||||
und dann aufrufen: |
und dann aufrufen: |
||||
|
|
||||
```Python |
```Python |
||||
user_dict = user_in.dict() |
user_dict = user_in.model_dump() |
||||
``` |
``` |
||||
|
|
||||
haben wir jetzt ein `dict` mit den Daten in der Variablen `user_dict` (es ist ein `dict` statt eines Pydantic-Modellobjekts). |
haben wir jetzt ein `dict` mit den Daten in der Variablen `user_dict` (es ist ein `dict` statt eines Pydantic-Modellobjekts). |
||||
@ -103,20 +95,20 @@ UserInDB( |
|||||
|
|
||||
#### Ein Pydantic-Modell aus dem Inhalt eines anderen { #a-pydantic-model-from-the-contents-of-another } |
#### Ein Pydantic-Modell aus dem Inhalt eines anderen { #a-pydantic-model-from-the-contents-of-another } |
||||
|
|
||||
Da wir im obigen Beispiel `user_dict` von `user_in.dict()` bekommen haben, wäre dieser Code: |
Da wir im obigen Beispiel `user_dict` von `user_in.model_dump()` bekommen haben, wäre dieser Code: |
||||
|
|
||||
```Python |
```Python |
||||
user_dict = user_in.dict() |
user_dict = user_in.model_dump() |
||||
UserInDB(**user_dict) |
UserInDB(**user_dict) |
||||
``` |
``` |
||||
|
|
||||
gleichwertig zu: |
gleichwertig zu: |
||||
|
|
||||
```Python |
```Python |
||||
UserInDB(**user_in.dict()) |
UserInDB(**user_in.model_dump()) |
||||
``` |
``` |
||||
|
|
||||
... weil `user_in.dict()` ein `dict` ist, und dann lassen wir Python es „entpacken“, indem wir es an `UserInDB` mit vorangestelltem `**` übergeben. |
... weil `user_in.model_dump()` ein `dict` ist, und dann lassen wir Python es „entpacken“, indem wir es an `UserInDB` mit vorangestelltem `**` übergeben. |
||||
|
|
||||
Auf diese Weise erhalten wir ein Pydantic-Modell aus den Daten eines anderen Pydantic-Modells. |
Auf diese Weise erhalten wir ein Pydantic-Modell aus den Daten eines anderen Pydantic-Modells. |
||||
|
|
||||
@ -125,7 +117,7 @@ Auf diese Weise erhalten wir ein Pydantic-Modell aus den Daten eines anderen Pyd |
|||||
Und dann fügen wir das zusätzliche Schlüsselwort-Argument `hashed_password=hashed_password` hinzu, wie in: |
Und dann fügen wir das zusätzliche Schlüsselwort-Argument `hashed_password=hashed_password` hinzu, wie in: |
||||
|
|
||||
```Python |
```Python |
||||
UserInDB(**user_in.dict(), hashed_password=hashed_password) |
UserInDB(**user_in.model_dump(), hashed_password=hashed_password) |
||||
``` |
``` |
||||
|
|
||||
... was so ist wie: |
... was so ist wie: |
||||
@ -180,7 +172,6 @@ Wenn Sie eine <a href="https://docs.pydantic.dev/latest/concepts/types/#unions" |
|||||
|
|
||||
{* ../../docs_src/extra_models/tutorial003_py310.py hl[1,14:15,18:20,33] *} |
{* ../../docs_src/extra_models/tutorial003_py310.py hl[1,14:15,18:20,33] *} |
||||
|
|
||||
|
|
||||
### `Union` in Python 3.10 { #union-in-python-3-10 } |
### `Union` in Python 3.10 { #union-in-python-3-10 } |
||||
|
|
||||
In diesem Beispiel übergeben wir `Union[PlaneItem, CarItem]` als Wert des Arguments `response_model`. |
In diesem Beispiel übergeben wir `Union[PlaneItem, CarItem]` als Wert des Arguments `response_model`. |
||||
@ -203,7 +194,6 @@ Dafür verwenden Sie Pythons Standard-`typing.List` (oder nur `list` in Python 3 |
|||||
|
|
||||
{* ../../docs_src/extra_models/tutorial004_py39.py hl[18] *} |
{* ../../docs_src/extra_models/tutorial004_py39.py hl[18] *} |
||||
|
|
||||
|
|
||||
## Response mit beliebigem `dict` { #response-with-arbitrary-dict } |
## Response mit beliebigem `dict` { #response-with-arbitrary-dict } |
||||
|
|
||||
Sie können auch eine Response deklarieren, die ein beliebiges `dict` zurückgibt, indem Sie nur die Typen der Schlüssel und Werte ohne ein Pydantic-Modell deklarieren. |
Sie können auch eine Response deklarieren, die ein beliebiges `dict` zurückgibt, indem Sie nur die Typen der Schlüssel und Werte ohne ein Pydantic-Modell deklarieren. |
||||
@ -214,7 +204,6 @@ In diesem Fall können Sie `typing.Dict` verwenden (oder nur `dict` in Python 3. |
|||||
|
|
||||
{* ../../docs_src/extra_models/tutorial005_py39.py hl[6] *} |
{* ../../docs_src/extra_models/tutorial005_py39.py hl[6] *} |
||||
|
|
||||
|
|
||||
## Zusammenfassung { #recap } |
## Zusammenfassung { #recap } |
||||
|
|
||||
Verwenden Sie gerne mehrere Pydantic-Modelle und vererben Sie je nach Bedarf. |
Verwenden Sie gerne mehrere Pydantic-Modelle und vererben Sie je nach Bedarf. |
||||
|
|||||
|
After Width: | Height: | Size: 187 KiB |
Loading…
Reference in new issue