FastAPI后台开发基础(5):使用PydanticModel定义参数类型

Pydantic Model类型 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 class Genders(str, Enum): Male = 'male' Female = 'female' class ExtendItem(BaseModel): a: str = '' b: int = 0 c: float = 0.1 d: bool = False class Config: """ 在 Pydantic 模型中,Config 类用于配置模型的行为。你可以在这个类中设置多种属性来调整模型的解析、验证和序列化行为。以下是一些常用的 Config 类字段: title: 用于为模型提供一个标题,通常用于生成的文档或模式中。 extra: 控制如何处理未在模型中声明的额外字段。可选值包括 ignore、allow、forbid。 arbitrary_types_allowed: 允许模型接受任意类型的字段,而不仅限于标准的 Pydantic 类型。 json_encoders: 为特定类型提供自定义的 JSON 编码器。 alias_generator: 生成别名的函数,用于字段名称,通常用于生成符合特定API规范的别名。 allow_population_by_field_name: 允许通过字段名称而不是别名来填充模型数据。 min_anystr_length: 字符串和字节类型字段的最小长度。 max_anystr_length: 字符串和字节类型字段的最大长度。 min_number_size: 数字类型字段的最小值。 max_number_size: 数字类型字段的最大值。 validate_assignment: 设置为 True 时,将在字段值被赋予新值后触发验证。 error_msg_templates: 自定义错误消息模板。 orm_mode: 允许模型与 ORM 模型兼容,通过允许使用非字典对象进行模型初始化。 use_enum_values: 当设置为 True 时,枚举字段将输出枚举成员的值而不是枚举成员本身。 anystr_strip_whitespace: 自动去除任何字符串或字节字段周围的空白。 schema_extra: 允许为 Pydantic 模型的 JSON Schema 添加额外的信息。 json_loads 和 json_dumps: 自定义 JSON 加载和转储函数。 keep_untouched: 保持某些类型在模型处理中不被修改,通常用于装饰器等高级用途。 """ extra = "allow" # 允许额外字段 class Item1(BaseModel): name: str = 'Bob' description: str price: float tax: int tags: list[str] = [] gender: Genders flags: Literal['test1', 'test2'] class Config: title = 'Item1' extra = "allow" # 允许额外字段 Model的使用 1 2 3 4 5 6 7 8 9 10 11 12 13 test: Item1 = Item1(name = 'Boc', description = 'test_desc', price = 1.23, tax = 123, tags = ['tag_1', 'tag_2'], gender = Genders.Male, flags = 'test1', extend = ExtendItem(a = 'a', b = 1, c = 0.1, d = True)) # 将 json string 转化为 model 对象 test = Item1.model_validate_json(test.model_dump_json(indent = 4)) # 将model对象打印成 json string print(test.model_dump_json(indent = 4)) GET请求的查询参数使用 model 类 1 2 3 4 5 6 7 8 9 10 11 12 13 @app.get("/get_model") async def get_model(items: Annotated[Item1, Query(description = 'GET 请求参数模型定义')]): """ GET请求不支持参数的嵌套 curl -X 'GET' \ 'http://127.0.0.1:18081/get_model?name=Bob&description=desc_test&price=1.23&tax=123&tags=tag_1&tags=tag_2&gender=male&flags=test1' \ -H 'accept: application/json' """ print(items) return { "method": "GET", "message": items } ...

2026-01-19 14:19 CST  · 1533 words  · 4 min

FastAPI后台开发基础(4):FastAPI官网文档错误、编码错误以及注意要点记录

设置查询参数是否为必填 使用Annotated装饰查询参数 不设置default值 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @app.get("/validation") async def async_root(str_param: Annotated[str | None, Query(min_length = 3, max_length = 20, pattern = '^[a-zA-Z0-9]+$', description = 'This is a string parameter')]): """ 如果不设置 str_param = None, 则 str_param 为必填 """ ret = {"str_list": str_param} if str_param is not None: h = hashlib.sha3_512(str_param.encode('utf-8')).hexdigest() ret.update({"sha3_512": h}) else: ret.update({"uuid": uuid.uuid4().hex}) return ret 不设置默认值 生成的文档中参数为必填 ...

2026-01-19 14:05 CST  · 1497 words  · 3 min

Oh My Zsh配置文档

1. 安装 Zsh macOS 1 2 which zsh brew install zsh Ubuntu / Debian 1 2 sudo apt update sudo apt install -y zsh CentOS / RHEL / Fedora 1 2 3 sudo yum install -y zsh # 或 sudo dnf install -y zsh 2. 设置 Zsh 为默认 Shell 1 2 3 4 5 6 # 当前会话切换 zsh # 设为默认 shell chsh -s "$(which zsh)" # 验证 echo $SHELL ⚠️ 若 chsh 失败,请确认: ...

2026-01-18 10:07 CST  · 1085 words  · 3 min

AWS去堡垒机化运维:基于IAM进行运维登录

在传统运维体系中,“登录服务器”通常意味着三件事: 一个固定账号 一组长期凭证 一个集中跳转点(堡垒机) 而在 AWS 的设计中,这套模型被系统性重构。 本文讨论的不是“如何配置某个功能”,而是一个更基础的问题: ...

2026-01-13 16:16 CST  · 1048 words  · 3 min

AWS SecretsManager的凭据生命周期管理

AWS在云原生架构中将凭据提升为一个具有完整生命周期的可信对象,它需要版本管理、定期轮转、权限审计和故障恢复策略。 一、Secrets Manager 凭据的静态与动态形态 Secrets Manager 用于管理客观存在、需要被系统记住的凭据,例如数据库密码、第三方 API Token 等。 ...

2026-01-13 14:08 CST  · 3881 words  · 8 min
文章 Posts 分类 Categories 标签 Tags