improving-drf-endpoints
Skill này làm gì
Skill này hướng dẫn AI xem xét và sửa các endpoint và serializer của Django REST Framework. Nó dành cho lập trình viên muốn tài liệu API và mã sinh tự động của họ chính xác.
Khi nào nên dùng
- ✓Khi bạn đang sửa một serializer hoặc viewset của Django REST Framework và muốn các trường có kiểu đúng.
- ✓Khi các kiểu API được sinh ra hiển thị giá trị lạ hoặc sai và bạn cần tìm nguyên nhân.
- ✓Khi bạn đang xem xét một thay đổi API và muốn kiểm tra mô tả trường và schema.
Liên kết skill
https://github.com/PostHog/posthog/blob/master/.agents/skills/improving-drf-endpoints/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Giúp tôi rà soát các serializer API Django của mình để tài liệu và kiểu được sinh ra hiển thị đúng trường và mô tả.”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Skill kiểm tra kiểu của từng trường, phần trợ giúp và ghi chú schema, vì các chi tiết này đi vào tài liệu API và mã sinh ra từ đó. Sau đó, skill đề xuất sửa mã serializer.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
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
Câu hỏi thường gặp
Tôi có cần biết Django để dùng không?
Có. Skill dành cho lập trình viên đã làm việc với Django REST Framework. Skill giải thích cần kiểm tra điều gì, nhưng bạn vẫn cần hiểu mã.
Lợi ích chính là gì?
Phần trợ giúp rõ ràng và kiểu trường đúng giúp tài liệu API và mã client được sinh ra mô tả chính xác từng endpoint.
Skill có hoạt động với mọi API không?
Skill nhắm đến các API Django REST Framework có sinh schema bằng drf-spectacular.
Chi tiết
Mô tả gốc của tác giả
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
Độ phổ biến
- Sao GitHub
- 41k
- Vị trí xếp hạng
- #6815
- Giấy phép
- —
- Cập nhật
- 2/11/2026
Bình luận
- …
Thêm skill từ kho mã này
depot-ci
41kGiúp agent của bạn thiết lập và chạy quy trình Depot CI, một lựa chọn thay thế GitHub Actions, gồm chuyển các quy trình hiện có và đọc nhật ký chạy.
depot-container-builds
41kGiúp agent của bạn build image Docker trên các builder từ xa của Depot, gồm image đa nền tảng, đẩy lên registry và sửa lỗi build.
depot-github-runners
41kGiúp agent của bạn chuyển các job GitHub Actions sang runner do Depot quản lý, chọn kích thước và nhãn runner, và xử lý lỗi thiết lập runner.
editing-agents-md
41kGiúp agent của bạn quyết định một quy tắc nên nằm trong file AGENTS.md hay CLAUDE.md, và viết sao cho quy tắc được tuân thủ mà file không bị phình to.
Skill tương tự
api-connector-builder
280kGiúp agent của bạn xây dựng kết nối API mới theo đúng mẫu đã có, dùng khi bạn cần thêm một tích hợp mà không tạo cách làm mới.
api-design
280kGiúp agent của bạn thiết kế REST API theo các thực hành tốt nhất, hữu ích để định nghĩa cách API giao tiếp, hiển thị lỗi và tổ chức dữ liệu.
backend-patterns
280kGiúp agent của bạn với các mẫu kiến trúc phía máy chủ và tối ưu cơ sở dữ liệu, hữu ích khi xây dựng hoặc rà soát code chạy trên máy chủ.