選項
首頁首頁 Skill 開發者工具 pydantic-models-py

pydantic-models-py

microsoft/skills microsoft/skills

使用 Pydantic v2 建立遵循多模型模式的 Pydantic 模型,包括 Base、Create、Update、Response 和 InDB 變體,以便在 Python 應用程式中實現清晰的 API 契約。

...展開全部
8
更新時間 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 將自動檢測並使用該技能。
儲存庫 microsoft/skills

相關技能

algorithmic-art
更新時間 2026-08-27
receiving-code-review
更新時間 2026-09-03
tech-debt-tracker
更新時間 2026-08-29
deprecation-and-migration
更新時間 2026-09-03
OR