选项
首页首页 Skill 开发者工具 pydantic-models-py

pydantic-models-py

microsoft/skills microsoft/skills

使用 Pydantic v2 创建遵循多模型模式的 Pydantic 模型,包括 Base、Create、Update、Response 和 InDB 变体,以便在 Python 应用程序中实现清晰的 API 契约。

...展开全部
7
更新时间 2026-09-12

Pydantic 模型

遵循多模型模式创建 Pydantic 模型,以构建清晰的 API 契约。

快速入门

assets/template.py 复制模板并替换占位符:

  • {{ResourceName}} → PascalCase 命名(例如,Project
  • {{resource_name}} → snake_case 命名(例如,project

多模型模式

模型用途
`Base`跨模型共享的公共字段
`Create`创建请求体(必填字段)
`Update`更新请求体(所有字段均为可选)
`Response`包含所有字段的 API 响应
`InDB`带有 `doc_type` 的数据库文档

camelCase 别名

class MyModel(BaseModel):
    workspace_id: str = Field(..., alias="workspaceId")
    created_at: datetime = Field(..., alias="createdAt")

    class Config:
        populate_by_name = True  # 同时接受 snake_case 和 camelCase

可选更新字段

class MyUpdate(BaseModel):
    """所有字段均为可选,用于 PATCH 请求。”"""
    name: Optional[str] = Field(None, min_length=1)
    description: Optional[str] = None

数据库文档

class MyInDB(MyResponse):
    """添加 doc_type 以支持 Cosmos DB 查询。”"""
    doc_type: str = "my_resource"

集成步骤

  1. src/backend/app/models/ 中创建模型
  2. src/backend/app/models/__init__.py 导出
  3. 添加相应的 TypeScript 类型
在 GitHub 上查看
---
name: pydantic-models-py
description: Create Pydantic models following the multi-model pattern with Base, Create, Update, Response, and InDB variants for clean API contracts in Python applications using Pydantic v2.
license: MIT
---

# Pydantic Models

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

## Quick Start

Copy the template from [assets/template.py](assets/template.py) and replace placeholders:
- `{{ResourceName}}` → PascalCase name (e.g., `Project`)
- `{{resource_name}}` → snake_case name (e.g., `project`)

## 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

```python
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

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

## Database Document

```python
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

所有文件

0 个文件

安装 pydantic-models-py

将技能文件下载并解压至你的 .claude/skills/ 目录。

下载ZIP

克隆仓库并复制技能文件到您的项目中。

git clone https://github.com/microsoft/skills/tree/main/.github/plugins/azure-sdk-python/skills/pydantic-models-py # Copy SKILL.md to your .claude/skills/ directory

复制 复制
快速设置: 将技能文件夹复制到 .claude/skills/ 目录。Claude 将自动检测并使用该技能。

相关技能

algorithmic-art
更新时间 2026-08-27
receiving-code-review
更新时间 2026-09-03
tech-debt-tracker
更新时间 2026-08-29
senior-backend
更新时间 2026-08-30
OR