Skillpedia
واجهات API والخوادم (backend)41k · #6815

improving-drf-endpoints

ما الذي تفعله

تُرشد هذه المهارة الذكاء الاصطناعي إلى مراجعة نقاط النهاية والمُسلسِلات في Django REST Framework وإصلاحها. وهي مناسبة للمطورين الذين يريدون أن تكون وثائق الواجهة والشيفرة المولّدة منها دقيقة.

متى تستخدمها

  • ✓عندما تعدّل مُسلسِلاً أو viewset في Django REST Framework وتريد تحديد أنواع حقوله بشكل صحيح.
  • ✓عندما تُظهر الأنواع المولّدة من الواجهة قيماً مجهولة أو خاطئة، وتحتاج إلى معرفة السبب.
  • ✓عندما تراجع تغييراً في الواجهة وتريد التحقق من أوصاف حقوله ومخططه.

رابط المهارة

https://github.com/PostHog/posthog/blob/master/.agents/skills/improving-drf-endpoints/SKILL.md

انسخه والصقه في الذكاء الاصطناعي الذي تستخدمه (Claude أو ChatGPT أو Cursor) واطلب منه اتباع التعليمات.

طريقة الاستخدام

أسهل طريقة: انسخ الرسالة والصقها في الذكاء الاصطناعي الذي تستخدمه. سيقرأ المهارة وينفّذ تعليماتها.

رسالة جاهزة للصق

استخدم هذه المهارة لمساعدتي: https://github.com/PostHog/posthog/blob/master/.agents/skills/improving-drf-endpoints/SKILL.md اقرأ ملف SKILL.md في هذا الرابط ونفّذ تعليماته لمهمتي.
تريد أن يجد الذكاء الاصطناعي مهارات مثل هذه بنفسه؟ثبّت خادم Skillpedia MCP المجاني: سطر واحد، ويبحث في 15,000+ مهارة قبل كل مهمة.ثبّته في دقيقة واحدة →

مثال على ما تطلبه من الذكاء الاصطناعي

“ساعدني في مراجعة مُسلسِلات Django الخاصة بي، كي تُظهر الوثائق والأنواع المولّدة الحقول والأوصاف الصحيحة.”

5 مهارات جديدة كل أسبوع. قراءة في دقيقة واحدة.

كيف تعمل

تتحقق المهارة من نوع كل حقل ونص المساعدة وملاحظات المخطط، لأن هذه التفاصيل تنتقل إلى وثائق الواجهة والشيفرة المولّدة منها. ثم تقترح تعديلات على شيفرة المُسلسِل.

المحتوى التقني (ملف 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 Serializer or ViewSet
  • 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 for unknown types (bare ListField/JSONField), missing JSDoc descriptions (missing help_text), or overly generic Record<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.

  1. Every field has help_text — describes purpose, format, constraints, valid values
  2. No bare ListField() or DictField() — always specify child= with a typed serializer or field
  3. No bare JSONField() — create a custom field class with @extend_schema_field(TypedSchema)
  4. SerializerMethodField has @extend_schema_field on its get_* method
  5. ChoiceField has explicit choices= with all valid values listed
  6. Define choices as a class — models.TextChoices in a product's internal modules, LabeledStrEnum or LabeledIntEnum from posthog/enums.py in 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, via ChoicesEnumNameOverrides in posthog/openapi/enum_names.py), so a class-backed enum never collides on field names like format, type, status, kind. Pass X.choices to choices=, and to name a labeled enum on a SerializerMethodField use @extend_schema_field(ChoiceField(choices=X.choices)), not a return type hint. Inline choices=[...] lists have no class to read, collide with existing choices, and fail CI under --fail-on-warn; an explicit ENUM_NAME_OVERRIDES entry in posthog/settings/web.py is the fallback for choice sets no class can carry (see serializer-fields.md)
  7. Read vs write serializers are separate when input shape differs from output
  8. 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

  1. Every custom @action has @extend_schema or @validated_request — without it, drf-spectacular discovers zero parameters
  2. Plain ViewSet methods have schema annotations — ModelViewSet with serializer_class is auto-discovered; plain ViewSet is not
  3. @extend_schema is on the actual method (get, post, create, list), not on a helper or the class itself
  4. Error responses are typed — use OpenApiResponse(response=ErrorSerializer), not OpenApiTypes.OBJECT
  5. List endpoints declare pagination — reset with pagination_class=None on custom actions that don't paginate
  6. Prefer @validated_request over manual serializer.is_valid() + @extend_schema — it handles both in one decorator
  7. **ViewSets o
الأسئلة الشائعة

هل أحتاج إلى معرفة Django لاستخدامها؟

نعم. المهارة موجهة للمطورين الذين يعملون بالفعل مع Django REST Framework. تشرح ما يجب فحصه، لكن عليك أن تفهم الشيفرة.

ما الفائدة الأساسية؟

نص المساعدة الواضح وأنواع الحقول الصحيحة يجعلان وثائق الواجهة والشيفرة التي يولّدها العميل تصف كل نقطة نهاية بدقة.

هل تعمل مع أي واجهة برمجية؟

هي موجهة لواجهات Django REST Framework التي تولّد مخططها باستخدام drf-spectacular.

التفاصيل

الوصف الأصلي من المؤلف

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
الترخيص
—
آخر تحديث
2‏/11‏/2026

هل هذه المهارة مفيدة؟

0

سجّل الدخول للتصويت.

التعليقات

  • …

المزيد من المهارات من هذا المستودع

مهارات مشابهة