蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

核对模型必填、序列化和响应过滤

PYTHONCPython 3.12、FastAPI 0.162.2、Uvicorn 0.54.0、pytest 9.2.0models.py
models.py34 行
from pydantic import BaseModel, ConfigDict, Field


class Observation(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None

    model_config = ConfigDict(json_schema_extra={
        "examples": [{"name": "东堤", "price": 12.5}],
    })


class RequiredNullable(BaseModel):
    description: str | None


class StationAlias(BaseModel):
    station_id: int = Field(alias="stationId")
    model_config = ConfigDict(from_attributes=True, validate_by_name=True, validate_by_alias=True)


class StationProfile(BaseModel):
    name: str


class StationInput(StationProfile):
    private_note: str


class StationOutput(StationProfile):
    id: int
本例需在 CPython 3.12、FastAPI 0.162.2、Uvicorn 0.54.0、pytest 9.2.0 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:CPython 3.12、FastAPI 0.162.2、Uvicorn 0.54.0、pytest 9.2.0

入口文件:projects/fastapi-request-lab/models.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 models.py,或下载右侧课程工程包。
  2. 确认本地已准备 CPython 3.12、FastAPI 0.162.2、Uvicorn 0.54.0、pytest 9.2.0。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:FastAPI

课程关联工程包(登记关联3章) · 含当前章节或实例 · mbskycom202611a506.zip

下载课程关联工程包:FastAPI

课程关联工程包(登记关联38章) · 含当前章节或实例 · mbskycom202612a182.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习