技能 pydantic-models-py
📦

pydantic-models-py

安全

使用多模型模式构建 Pydantic 模型

无需手动编写样板代码即可定义一致的 API 架构。此技能可生成结构化的 Pydantic 模型,遵循已建立的请求验证、响应和数据库集成模式。

支持: Claude Codex Code(CC)
🥉 74 青铜
1

下载技能 ZIP

2

在 Claude 中上传

前往 设置 → 功能 → 技能 → 上传技能

3

开启并开始使用

测试它

正在使用“pydantic-models-py”。 User entity with id, name, email, created_at fields

预期结果:

生成具有正确 Field 定义、类型注解和 Config 类的 UserBase、UserCreate、UserUpdate、UserResponse 和 UserInDB 模型类,用于别名处理

正在使用“pydantic-models-py”。 Project model requiring camelCase API compatibility

预期结果:

具有 Field 别名(workspaceId、createdAt)的模型,可接受两种命名约定,同时在 Python 代码内部使用 Pythonic 的 snake_case

安全审计

安全
v1 • 2/24/2026

All 21 static analysis findings are false positives. The scanner misidentified Markdown code block backticks as shell execution, and documentation references to HTTP/cryptography as actual code. SKILL.md is pure documentation with Python code examples for Pydantic model patterns. No executable code, network calls, or security risks detected.

1
已扫描文件
64
分析行数
0
发现项
1
审计总数
未发现安全问题
审计者: claude

质量评分

38
架构
100
可维护性
87
内容
50
社区
100
安全
91
规范符合性

你能构建什么

REST API 架构定义

为 FastAPI 或 Flask 端点定义请求/响应模型,使用一致的验证模式。

数据库文档建模

创建带有 doc_type 字段的 InDB 模型变体,用于 Cosmos DB 或 MongoDB 文档存储。

前端-后端契约同步

生成匹配的 Python 模型和 TypeScript 类型以确保 API 契约一致性。

试试这些提示

基础模型生成
Create Pydantic models for a User entity with fields: id (string), name (string, required), email (string with email validation), created_at (datetime). Use the multi-model pattern with Base, Create, Update, and Response variants.
带 CamelCase 别名的模型
Generate Pydantic models for a Project entity. All fields should accept both snake_case and camelCase using Field aliases. Include workspace_id, project_name, and is_active fields.
带 InDB 变体的数据库模型
Create complete Pydantic models for a Document entity including InDB variant with doc_type field. Add min_length validation on name and optional description field for updates.
复杂嵌套模型模式
Build Pydantic models for an Order entity with nested line_items array. Each line item has product_id, quantity, and price. Create full multi-model pattern with validation constraints on all fields.

最佳实践

  • 始终分离 Create 和 Update 模型 - Create 需要所有字段,Update 将所有字段设为可选以支持 PATCH 语义
  • 在可选更新字段上使用 explicit default=None 的 Field,以区分 null 和未设置的值
  • 在 Config 类中启用 populate_by_name = True,以接受来自 API 客户端的 snake_case 和 camelCase 两种命名方式

避免

  • 不要将同一模型类重用于请求体和数据库文档 - 分离可防止数据泄露
  • 避免在模型类中放置业务逻辑 - 将它们保持为仅包含验证的纯数据架构
  • 在使用 Cosmos DB 时不要在 InDB 模型中省略 doc_type - 没有正确的类型过滤,查询将失败

常见问题

什么是多模型模式?为什么使用它?
该模式为不同用途创建单独的模型类:Base(共享字段)、Create(POST 所需字段)、Update(PATCH 的可选字段)、Response(完整输出)和 InDB(带元数据的数据库文档)。这种分离可防止意外数据泄露,并在每一层强制执行正确的验证。
camelCase 别名如何与 Pydantic 配合使用?
在每个字段上使用 Field(alias='camelCaseName'),并在 Config 类中设置 populate_by_name = True。这允许模型接受来自 API 客户端的两种命名约定,同时在 Python 代码内部使用 snake_case。
为什么将所有 Update 字段设为可选?
PATCH 请求应仅包含正在修改的字段。将所有字段设为可选并设置 default=None 即可支持部分更新,同时区分'未提供字段'(None)和'字段设置为 null'(显式 null)。
doc_type 字段用于什么?
在 Cosmos DB 等文档数据库中,doc_type 标识共享集合中的实体类型。InDB 模型自动添加此字段,以便查询可以按类型过滤文档。
我需要手动创建 TypeScript 类型吗?
是的,此技能会引导您创建匹配的 TypeScript 接口。考虑使用 pydantic2ts 或 datamodel-code-generator 等工具从 Pydantic 模型自动生成 TypeScript。
我可以将此模式与 SQLAlchemy 或其他 ORM 一起使用吗?
是的,Pydantic 模型可与 ORM 配合使用。将 Pydantic 用于请求/响应验证和序列化,将 ORM 模型用于数据库操作。某些 ORM(如 SQLAlchemy 2.0)具有原生 Pydantic 集成。

开发者详情

文件结构

📄 SKILL.md