Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌐 Add Chinese translation for docs/zh/docs/tutorial/dependencies/classes-as-dependencies.md #4971

Merged
247 changes: 247 additions & 0 deletions docs/zh/docs/tutorial/dependencies/classes-as-dependencies.md
@@ -0,0 +1,247 @@
# 类作为依赖项

在深入探究**依赖注入**系统之前,让我们升级之前的例子。
Zssaer marked this conversation as resolved.
Show resolved Hide resolved

## 来自前一个例子的`dict`

在前面的例子中, 我们从可依赖项中返回了一个`dict`:
Zssaer marked this conversation as resolved.
Show resolved Hide resolved
Zssaer marked this conversation as resolved.
Show resolved Hide resolved

=== "Python 3.6 以及 以下"
Zssaer marked this conversation as resolved.
Show resolved Hide resolved

```Python hl_lines="9"
{!> ../../../docs_src/dependencies/tutorial001.py!}
```

=== "Python 3.10 以及以上"

```Python hl_lines="7"
{!> ../../../docs_src/dependencies/tutorial001_py310.py!}
```

但是后面我们在路径操作函数的参数`commons `共享中得到了一个`dict`。
Zssaer marked this conversation as resolved.
Show resolved Hide resolved

我们知道编辑器不能为`dict`提供很多支持(比如补全),因为它们不能知道它们的键和值类型。
Zssaer marked this conversation as resolved.
Show resolved Hide resolved

对此,我们可以做的更好...

## 什么构成了依赖项?

到目前为止,您看到的依赖项都被声明为函数。

但这并不是声明依赖项的唯一方法(尽管它可能是更常见的方法)。

关键因素是依赖项应该是“可调用的”。
Zssaer marked this conversation as resolved.
Show resolved Hide resolved

Python中的“**可调用**”是指任何Python可以像函数一样“调用”的对象。
Zssaer marked this conversation as resolved.
Show resolved Hide resolved

所以,如果你需要一个对象`something`(可能*不是*一个函数),你可以像这样“调用”它(执行它),就像:
Zssaer marked this conversation as resolved.
Show resolved Hide resolved

```Python
something()
```

或者

```Python
something(some_argument, some_keyword_argument="foo")
```

像这样它就是“可调用”。
Zssaer marked this conversation as resolved.
Show resolved Hide resolved

## 类做依赖项
Zssaer marked this conversation as resolved.
Show resolved Hide resolved

您可能会注意到,要创建一个Python类的实例,您可以使用相同的语法。
Zssaer marked this conversation as resolved.
Show resolved Hide resolved

举个例子:

```Python
class Cat:
def __init__(self, name: str):
self.name = name


fluffy = Cat(name="Mr Fluffy")
```

在这个例子中, `fluffy` 是一个实现`Cat`的实例。
Zssaer marked this conversation as resolved.
Show resolved Hide resolved

并且为了创建`fluffy`,你就调用了`Cat`。
Zssaer marked this conversation as resolved.
Show resolved Hide resolved

因此,Python类也是属于**可调用**。
Zssaer marked this conversation as resolved.
Show resolved Hide resolved

所以,在**FastAPI**中,你可以使用Python类作为依赖项。
Zssaer marked this conversation as resolved.
Show resolved Hide resolved

FastAPI实际上检查的是它是否“可调用”(函数,类或其他任何类型)以及定义的参数。
Zssaer marked this conversation as resolved.
Show resolved Hide resolved

如果您在**FastAPI**中以一个“可调用”对象作为依赖项,它将分析该“可调用”对象的参数,并以与路径操作函数的参数相同的方式处理它们。包括子依赖项。
Zssaer marked this conversation as resolved.
Show resolved Hide resolved

这也适用于完全没有参数的可调用对象。这与不带参数的路径操作函数一样。

所以,我们可以将上面的依赖项`common_parameters`更改为类`CommonQueryParams`
Zssaer marked this conversation as resolved.
Show resolved Hide resolved

=== "Python 3.6 以及 以下"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
=== "Python 3.6 以及 以下"
=== "Python 3.6 以及 以上"


```Python hl_lines="11-15"
{!> ../../../docs_src/dependencies/tutorial002.py!}
```

=== "Python 3.10 以及 以上"

```Python hl_lines="9-13"
{!> ../../../docs_src/dependencies/tutorial002_py310.py!}
```

注意用于创建类实例的`__init__`方法:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
注意用于创建类实例的`__init__`方法:
注意用于创建类实例的 `__init__` 方法:


=== "Python 3.6 以及 以下"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
=== "Python 3.6 以及 以下"
=== "Python 3.6 以及 以上"


```Python hl_lines="12"
{!> ../../../docs_src/dependencies/tutorial002.py!}
```

=== "Python 3.10 以及 以上"

```Python hl_lines="10"
{!> ../../../docs_src/dependencies/tutorial002_py310.py!}
```

...它具有与我们以前的`common_parameters`相同的参数:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
...它具有与我们以前的`common_parameters`相同的参数
...它与我们以前的 `common_parameters` 具有相同的参数


=== "Python 3.6 以及 以下"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
=== "Python 3.6 以及 以下"
=== "Python 3.6 以及 以上"


```Python hl_lines="9"
{!> ../../../docs_src/dependencies/tutorial001.py!}
```

=== "Python 3.10 以及 以上"

```Python hl_lines="6"
{!> ../../../docs_src/dependencies/tutorial001_py310.py!}
```

这些参数就是**FastAPI**用来“处理”依赖项的。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
这些参数就是**FastAPI**用来“处理”依赖项的。
这些参数就是 **FastAPI** 用来 "处理" 依赖项的。


在这两种创建依赖项方法下,都将会:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
在这两种创建依赖项方法下,都将会
在两个例子下,都有


* 一个可选的`q`查询参数,它是一个`str`类型。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 一个可选的`q`查询参数,它是一个`str`类型。
* 一个可选的 `q` 查询参数,`str` 类型。

* 一个整数类型的`skip`查询参数,默认值为0。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 一个整数类型的`skip`查询参数,默认值为0
* 一个 `skip` 查询参数,`int` 类型,默认值为 `0`

* 一个整数类型`limit`查询参数,默认值为0。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 一个整数类型`limit`查询参数,默认值为0
* 一个 `limit` 查询参数,`int` 类型,默认值为 `100`


在这两种创建依赖项方法下,数据都将在OpenAPI概图上进行转换、验证和记录等。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
在这两种创建依赖项方法下,数据都将在OpenAPI概图上进行转换、验证和记录等
在两个例子下,数据都将被转换、验证、在 OpenAPI schema 上文档化,等等


## 使用它

现在,您可以使用此类声明的依赖项了。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
现在,您可以使用此类声明的依赖项了
现在,您可以使用这个类来声明你的依赖项了


=== "Python 3.6 以及 以下"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
=== "Python 3.6 以及 以下"
=== "Python 3.6 以及 以上"


```Python hl_lines="19"
{!> ../../../docs_src/dependencies/tutorial002.py!}
```

=== "Python 3.10 以及 以上"

```Python hl_lines="17"
{!> ../../../docs_src/dependencies/tutorial002_py310.py!}
```

**FastAPI** 调用这个`CommonQueryParams`类。这将创建该类的一个“实例”,该实例将作为`commons`参数传递给你的函数。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
**FastAPI** 调用这个`CommonQueryParams`类。这将创建该类的一个“实例”,该实例将作为`commons`参数传递给你的函数
**FastAPI** 调用 `CommonQueryParams` 类。这将创建该类的一个 "实例",该实例将作为参数 `commons` 被传递给你的函数


## 类型注解 vs `Depends`

注意,我们在上面的代码中编写了两次`CommonQueryParams`:

```Python
commons: CommonQueryParams = Depends(CommonQueryParams)
```

最后的 `CommonQueryParams`:

```Python
... = Depends(CommonQueryParams)
```

...**Fastapi**实际上将知道使用什么依赖项。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
...**Fastapi**实际上将知道使用什么依赖项
...实际上是 **Fastapi** 用来知道依赖项是什么的


FastAPI将从中提取声明的参数,这才是FastAPI实际调用的。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
FastAPI将从中提取声明的参数,这才是FastAPI实际调用的
FastAPI 将从依赖项中提取声明的参数,这才是 FastAPI 实际调用的


---

在本例中,第一个`CommonQueryParams`:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
在本例中,第一个`CommonQueryParams`
在本例中,第一个 `CommonQueryParams`


```Python
commons: CommonQueryParams ...
```

...对于**FastAPI**没有任何特殊的意义。FastAPI不会使用它进行数据转换、验证等(因为对于这,它实际使用的是`= Depends(CommonQueryParams)`)。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
...对于**FastAPI**没有任何特殊的意义。FastAPI不会使用它进行数据转换、验证等因为对于这,它实际使用的是`= Depends(CommonQueryParams)`
...对于 **FastAPI** 没有任何特殊的意义。FastAPI 不会使用它进行数据转换、验证等 (因为对于这,它使用 `= Depends(CommonQueryParams)`)


你实际上可以只这样编写:

```Python
commons = Depends(CommonQueryParams)
```

..就像这样:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
..就像这样:
..就像:


=== "Python 3.6 以及 以下"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
=== "Python 3.6 以及 以下"
=== "Python 3.6 以及 以上"


```Python hl_lines="19"
{!> ../../../docs_src/dependencies/tutorial003.py!}
```

=== "Python 3.10 以及 以上"

```Python hl_lines="17"
{!> ../../../docs_src/dependencies/tutorial003_py310.py!}
```

但是声明类型是被鼓励的,因为这样你的编辑器就会知道将传递什么作为`commons`参数,然后它可以帮助你完成代码,类型检查,等等:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
但是声明类型是被鼓励的,因为这样你的编辑器就会知道将传递什么作为`commons`参数,然后它可以帮助你完成代码,类型检查,等等:
但是声明类型是被鼓励的,因为那样你的编辑器就会知道将传递什么作为参数 `commons` ,然后它可以帮助你完成代码,类型检查,等等:


<img src="/img/tutorial/dependencies/image02.png">

## 快捷方式

但是您可以看到,我们在这里有一些代码重复了,编写了`CommonQueryParams`两次:

```Python
commons: CommonQueryParams = Depends(CommonQueryParams)
```

**FastAPI**为这些情况提供了一个快捷方式,在这种情况下,依赖项是一个特定的类,**FastAPI**将其“调用”来创建类本身的一个实例。
Zssaer marked this conversation as resolved.
Show resolved Hide resolved

对于这些特定的情况,您可以跟随以下操作:

不是写成这样:

```Python
commons: CommonQueryParams = Depends(CommonQueryParams)
```

...而是这样写:

```Python
commons: CommonQueryParams = Depends()
```

您声明依赖项作为参数的类型,并使用`Depends()`作为该函数的参数的“默认”值(在=之后),而在`Depends()`中没有任何参数,这样不必在内部再次编写完整的类,取代了`Depends(CommonQueryParams)`。
Zssaer marked this conversation as resolved.
Show resolved Hide resolved

同样的例子看起来像这样:

=== "Python 3.6 以及 以下"
Zssaer marked this conversation as resolved.
Show resolved Hide resolved

```Python hl_lines="19"
{!> ../../../docs_src/dependencies/tutorial004.py!}
```

=== "Python 3.10 以及 以上"

```Python hl_lines="17"
{!> ../../../docs_src/dependencies/tutorial004_py310.py!}
```

...**Fastapi**将会知道该怎么处理。
Zssaer marked this conversation as resolved.
Show resolved Hide resolved

!!! tip
如果这看起来更加混乱而不是更加有帮助,那么请忽略它,你不*需要*它。

这只是一个快捷方式。因为**FastAPI**关心的是帮助您减少代码重复。
Zssaer marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions docs/zh/mkdocs.yml
Expand Up @@ -86,6 +86,7 @@ nav:
- tutorial/body-updates.md
- 依赖项:
- tutorial/dependencies/index.md
- tutorial/dependencies/classes-as-dependencies.md
- tutorial/dependencies/sub-dependencies.md
- tutorial/dependencies/dependencies-in-path-operation-decorators.md
- tutorial/dependencies/global-dependencies.md
Expand Down