improving-drf-endpoints
它能做什么
本技能指导 AI 审查并修复 Django REST Framework 的端点和序列化器。适合希望 API 文档和生成代码准确无误的开发者。
适用场景
- ✓当你在编辑 Django REST Framework 的序列化器或视图集,并希望正确设置其字段类型的时候。
- ✓当生成的 API 类型显示未知或错误的值,需要查找原因时。
- ✓当你在审查 API 改动,并希望检查其字段描述和结构的时候。
技能链接
https://github.com/PostHog/posthog/blob/master/.agents/skills/improving-drf-endpoints/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请帮我审查 Django API 的序列化器,让生成的文档和类型显示正确的字段与描述。”
每周 5 个新技能,1 分钟读完。
工作原理
它会检查每个字段的类型、帮助文本和结构说明,因为这些细节会传递到 API 文档以及由此生成的代码中。然后,它会建议对序列化器代码进行修改。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Improving DRF Endpoints
Before you propose a contract test against the generated OpenAPI schema, check things already tried. Six PRs took that idea, and none merged.
Overview
Serializer fields are the source of truth for PostHog's entire type pipeline:
Django serializer → drf-spectacular → OpenAPI JSON → Orval → Zod schemas → MCP tools
Every help_text, every field type, every @extend_schema annotation flows downstream.
A missing help_text means an agent guessing at parameters.
A bare ListField() means z.unknown() in the generated Zod schema.
Getting the serializer right means every consumer — frontend types, MCP tools, API docs — gets correct types and descriptions automatically.
When to use
- Editing or reviewing any file that defines a
SerializerorViewSet - Fixing OpenAPI spec warnings or generated type issues
- Preparing an endpoint for MCP tool exposure
- Code review of API changes
Audit checklist
Triage: check the generated output first
Before diving into Python, look at the committed generated types to see what's broken. Find the generated files for the endpoint's product:
- Core API:
frontend/src/generated/core/ - Product APIs:
products/<product>/frontend/generated/
Each has two files:
api.schemas.ts— TypeScript interfaces derived from serializers. Search for the serializer name and look forunknowntypes (bareListField/JSONField), missing JSDoc descriptions (missinghelp_text), or overly genericRecord<string, unknown>shapes.api.ts— API client functions. Check if the endpoint's operation exists at all — if missing, the viewset method likely lacks@extend_schema.
This tells you exactly which fields and endpoints to prioritize.
Serializer fields
Work through this list for every serializer and viewset you touch.
- Every field has
help_text— describes purpose, format, constraints, valid values - No bare
ListField()orDictField()— always specifychild=with a typed serializer or field - No bare
JSONField()— create a custom field class with@extend_schema_field(TypedSchema) SerializerMethodFieldhas@extend_schema_fieldon itsget_*methodChoiceFieldhas explicitchoices=with all valid values listed- Define choices as a class —
models.TextChoicesin a product's internal modules,LabeledStrEnumorLabeledIntEnumfromposthog/enums.pyin facade contract files (backend/facade/contracts.py,backend/facade/enums.py), which must not import Django. The OpenAPI component is named after the class either way (EarlyAccessFeature.Stage->EarlyAccessFeatureStageEnum, viaChoicesEnumNameOverridesinposthog/openapi/enum_names.py), so a class-backed enum never collides on field names likeformat,type,status,kind. PassX.choicestochoices=, and to name a labeled enum on aSerializerMethodFielduse@extend_schema_field(ChoiceField(choices=X.choices)), not a return type hint. Inlinechoices=[...]lists have no class to read, collide with existing choices, and fail CI under--fail-on-warn; an explicitENUM_NAME_OVERRIDESentry inposthog/settings/web.pyis the fallback for choice sets no class can carry (see serializer-fields.md) - Read vs write serializers are separate when input shape differs from output
- Every success response is backed by a serializer — returning raw dicts or untyped lists means no generated types downstream
See serializer-fields.md for patterns and examples.
Viewset and action annotations
- Every custom
@actionhas@extend_schemaor@validated_request— without it, drf-spectacular discovers zero parameters - Plain
ViewSetmethods have schema annotations —ModelViewSetwithserializer_classis auto-discovered; plainViewSetis not @extend_schemais on the actual method (get,post,create,list), not on a helper or the class itself- Error responses are typed — use
OpenApiResponse(response=ErrorSerializer), notOpenApiTypes.OBJECT - List endpoints declare pagination — reset with
pagination_class=Noneon custom actions that don't paginate - Prefer
@validated_requestover manualserializer.is_valid()+@extend_schema— it handles both in one decorator - **ViewSets o
常见问题
使用它需要了解 Django 吗?
需要。它面向已经在使用 Django REST Framework 的开发者。它会说明应检查哪些内容,但你仍需理解代码。
它的主要好处是什么?
清晰的帮助文本和正确的字段类型,能让生成的 API 文档和客户端代码准确描述每个端点。
它适用于任何 API 吗?
它主要针对使用 drf-spectacular 生成结构的 Django REST Framework API。
详情
作者的原始描述
Use when editing, reviewing, or auditing DRF viewsets and serializers in PostHog. Triggers on files in posthog/api/, products/*/backend/api/, products/*/backend/presentation/, or any file importing rest_framework. Covers field typing, schema annotations, enum collision fixes, and OpenAPI spec quality — everything that flows downstream into generated TypeScript types and MCP tools.
repo: PostHog/posthog
热度
- GitHub 星标
- 41k
- 排名
- #6815
- 许可证
- —
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
depot-ci
41k帮你的智能体设置并运行 Depot CI 工作流(GitHub Actions 的替代方案),包括迁移现有工作流和读取运行日志。
depot-container-builds
41k帮你的智能体在 Depot 的远程构建器上构建 Docker 镜像,包括多平台镜像、推送到镜像仓库,以及修复构建失败。
depot-github-runners
41k帮你的智能体把 GitHub Actions 任务迁移到 Depot 托管的运行器上,选择运行器规格和标签,并排查运行器设置问题。
editing-agents-md
41k帮你的智能体判断某条规则应写入 AGENTS.md 还是 CLAUDE.md,并以不会让文件臃肿的方式写下,确保规则真正生效。