/claude-kit:django-rest-framework-patternsfrom Claude Kit SDLC
Standardizes Django REST Framework patterns: ViewSet vs APIView, serializer boundaries, queryset shaping to prevent N+1, and wiring OpenAPI, filtering, JWT auth, and pagination.
Ships in Claude Kit SDLC. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Standardize the DRF layer: what belongs in a ViewSet, what belongs in a serializer, and where the queryset gets shaped so the API does not issue a query per row.
Targets Django 6.1 with DRF. Django Ninja is covered as the documented alternative, since cookiecutter-django offers both.
Every DRF request walks the same path. Knowing the order tells you where a fix belongs.
HTTP request
→ Router resolves the URL to a ViewSet action
→ ViewSet permission_classes, then authentication
→ get_queryset() the base rows, and where select_related/prefetch_related go
→ filter_backends django-filter, search, ordering
→ Serializer field selection and validation — the API contract
→ Response rendered
Two rules follow directly:
get_queryset(). It is the only step that runs before the serializer
touches a row.obj.related.all() runs once per
row, and no amount of caching downstream fixes it.class OrderViewSet(viewsets.ModelViewSet):
serializer_class = OrderSerializer
permission_classes = [IsAuthenticated]
filterset_fields = ["status", "customer"]
def get_queryset(self):
return (
Order.objects
.filter(customer__org=self.request.user.org) # tenancy first
.select_related("customer") # FK / O2O
.prefetch_related("items__product") # M2M / reverse FK
)
def perform_create(self, serializer):
create_order(user=self.request.user, **serializer.validated_data)
The view parses, authorises, and delegates. The workflow lives in services.py (see
django-service-patterns), which keeps it callable from a management command or a Celery task.
Use ModelViewSet when the resource really is CRUD. Reach for APIView when the endpoint is an
action rather than a resource — POST /orders/{id}/refund is clearer as an explicit view or a
@action than as a contorted partial_update.
fields explicitly. fields = "__all__" means the next model field you add is
published to the API without anyone deciding to.validate_<field> and validate; both raise serializers.ValidationError, which
DRF maps to 400.get_serializer_class(). Forcing one
class to do both produces a thicket of read_only and required=False.The failure mode is always the same: a serializer with a nested field, and a queryset that did not prefetch it. One query becomes one-per-row and the endpoint degrades with page size.
Fix it in get_queryset():
| Relation | Use |
|---|---|
| Forward FK, one-to-one | select_related (SQL join, one query) |
| Reverse FK, many-to-many | prefetch_related (second query, joined in Python) |
| Nested prefetch with its own filter | Prefetch("items", queryset=Item.objects.select_related("product")) |
Django 6.1 adds fetch modes, where FETCH_PEERS collapses most unplanned N+1 into two
queries. Treat it as a backstop for the case you missed, not a reason to stop declaring what the
endpoint needs — an explicit select_related still documents intent and survives an ORM upgrade.
Verify with django-debug-toolbar locally, or assert query counts in tests:
def test_order_list_is_constant_query_count(client, django_assert_num_queries):
with django_assert_num_queries(4):
client.get("/api/orders/")
A count assertion is the only thing that keeps an N+1 fix from silently regressing.
DEFAULT_PAGINATION_CLASS and PAGE_SIZE globally; an
unpaginated list endpoint is a production incident waiting for the table to grow.
LimitOffsetPagination is fine for admin surfaces; cursor pagination is the one that stays
correct while rows are being inserted.DjangoFilterBackend and filterset_fields, so filters are declared
rather than parsed out of request.query_params by hand.django-react-integration.CorsMiddleware must sit
above CommonMiddleware.drf-spectacular generates OpenAPI 3 from the serializers and view annotations, and that schema is
what generates a typed client for the frontend. Annotate anything it cannot infer:
@extend_schema(responses=RefundSerializer, request=RefundRequestSerializer)
def refund(self, request, pk=None): ...
Generate the schema in CI and fail on a diff, the same way migrations are checked — otherwise the published contract drifts from the code that serves it.
Django Ninja is async-first and uses Pydantic v2 for schemas, so it feels like FastAPI inside Django. It is a reasonable choice for a new async-heavy API.
Choose DRF when you want the larger ecosystem (ViewSets, permissions, browsable API, the filter and
auth packages above) or the team already knows it. Choose Ninja when the API is async throughout
and you would otherwise be fighting DRF's sync design — DRF has no first-class async support,
and the adrf bridge has real limits (see django-async-patterns).
Do not run both in one project. Two API frameworks means two auth integrations, two schema generators, and two sets of conventions.
fields = "__all__" — publishes every future model field, including the one you add for
internal bookkeeping.create() / update() — unreachable from anything that is not an HTTP
request.request.query_params — undocumented, unvalidated, and invisible to
the OpenAPI schema.get_queryset(), before any
row is loaded. Filtering after the fact has already leaked the row.AllowAny as the project default permission — make the default restrictive and open
endpoints deliberately.django-service-patterns — where the services layer these views call belongsdjango-async-patterns — async DRF via adrf, and its limitsdjango-react-integration — session vs JWT, CORS ordering, generated TypeScript clientsapi-and-interface-design — the stack-agnostic API contract rules, installed alongside this skillWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub ajyadav013/claude-kit --plugin claude-kit127 skills in this plugin
/accessibility-reviewUI accessibility (WCAG) review — keyboard operability, focus, ARIA semantics, color contrast, touch targets. Returns prioritized fixes. Distinct from performance/visual review./alembic-migrationsAlembic migration setup and patterns for async SQLAlchemy — async engine + run_sync in env.py, autogenerate, RLS/multi-tenant migrations. Use for alembic init, writing migrations, or debugging migration errors./anthropic-vertex-integrationClaude on Vertex AI via the AnthropicVertex SDK — lazy ADC client init, env-based project/region resolution, async helpers, backoff retry. Use when calling Claude via GCP Vertex AI (not direct Anthropic API).Other plugins' skills for the same job