跳到正文
Skillpedia
API 与服务器(后端)48k · #5542

pydantic-models-py

它能做什么

这个工具可以帮你创建有组织的数据结构,用于计算机程序中,特别是系统不同部分之间的通信(比如 API)。它采用一种称为“多模型模式”(Multi-Model Pattern)的方法来定义数据的形态。

适用场景

  • ✓当你需要定义创建某项内容时发送的数据格式时。
  • ✓当你需要定义更新已有内容时发送的数据格式时。
  • ✓当你需要定义以响应形式返回的数据格式时。

技能链接

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/pydantic-models-py/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/pydantic-models-py/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请按照多模型模式,为用户创建一个 Pydantic 模型,包含 name、email 和创建日期(creation date)字段。”

每周 5 个新技能,1 分钟读完。

工作原理

这个工具借助 Pydantic 库定义“模型”,你可以把它们理解为组织数据的蓝图。对于通用数据,它建议使用基础模型;对于创建、更新和响应等场景,则分别使用对应的专用模型。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Pydantic Models

Create Pydantic models following the multi-model pattern for clean API contracts.

Quick Start

Use the inline model patterns below and adapt class names and fields to the actual API contract. Inspect the installed Pydantic version before selecting configuration syntax; these fragments require the imports and application types shown by your project. Do not assume a standalone template file is bundled.

Multi-Model Pattern

Model Purpose
Base Common fields shared across models
Create Request body for creation (required fields)
Update Request body for updates (all optional)
Response API response with all fields
InDB Database document with doc_type

camelCase Aliases

class MyModel(BaseModel):
    workspace_id: str = Field(..., alias="workspaceId")
    created_at: datetime = Field(..., alias="createdAt")
    
    class Config:
        populate_by_name = True  # Accept both snake_case and camelCase

Optional Update Fields

class MyUpdate(BaseModel):
    """All fields optional for PATCH requests."""
    name: Optional[str] = Field(None, min_length=1)
    description: Optional[str] = None

Database Document

class MyInDB(MyResponse):
    """Adds doc_type for Cosmos DB queries."""
    doc_type: str = "my_resource"

Integration Steps

  1. Create models in src/backend/app/models/
  2. Export from src/backend/app/models/__init__.py
  3. Add corresponding TypeScript types

When to Use

This skill is applicable to execute the workflow or actions described in the overview.

Limitations

  • Use this skill only when the task clearly matches the scope described above.
  • Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
  • Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.
常见问题

什么是 Pydantic?

Pydantic 是一个 Python 库,它利用类型注解来帮助你校验和组织数据。

什么是“多模型模式”?

这是一种按用途组织数据模型的方法,比如分别用于创建、更新或响应,从而让代码更清晰、更一致。

“camelCase 别名”有什么用?

它让你的模型既能接收 snake_case 格式(如 `workspace_id`)的字段名,也能接收 camelCase 格式(如 `workspaceId`)的字段名,使系统集成更加灵活。

详情

作者的原始描述

Create Pydantic models following the multi-model pattern for clean API contracts.

repo: sickn33/agentic-awesome-skills

热度

GitHub 星标
48k
排名
#5542
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能