Home › Django › Django REST Framework rules

Django REST Framework rules

Cursor rules for Django 5 + Django REST Framework APIs: serializers, viewsets, permissions, and query performance.

CursorDjango

How to use

Save this as .cursor/rules/drf.mdc in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

.cursor/rules/drf.mdcDownload  
---
description: Django REST Framework conventions
globs: ["**/*.py"]
alwaysApply: false
---

# Django REST Framework

- Use `ModelViewSet`/`GenericViewSet` + mixins and register with a router. Function views only for one-off endpoints.
- Separate read and write serializers when shapes differ. Validation lives in `validate_<field>` / `validate`; business logic in a `services.py` function the view calls.
- Every view sets `permission_classes` explicitly; default to `IsAuthenticated` in settings. Object-level checks via `has_object_permission`.
- Scope querysets in `get_queryset()` to the requesting user/tenant. Never return `Model.objects.all()` for user data.
- Prevent N+1: `select_related` for FKs, `prefetch_related` for reverse/M2M, and check with `django-debug-toolbar` or `assertNumQueries` in tests.
- Pagination on every list endpoint (global `DEFAULT_PAGINATION_CLASS`). Filtering via `django-filter`.
- Document with `drf-spectacular`; add `@extend_schema` where inference is wrong.
- Wrap multi-write operations in `transaction.atomic()`.
- Throttle auth and expensive endpoints.

## Don't
- Don't use `fields = '__all__'` on serializers that write data.
- Don't put logic in `serializer.save()` overrides that should be a service.

## Done when
`python manage.py test` (or `pytest`) passes, `python manage.py makemigrations --check` is clean, and `ruff check .` passes.

Related rules

Astro content site rules

Cursor rules for Astro 5 sites with content collections, islands, and zero-JS-by-default pages.

CursorAstro

Chrome extension (Manifest V3) rules

Cursor rules for building Manifest V3 browser extensions with TypeScript and minimal permissions.

CursorChrome extensions

Cloudflare Agents SDK rules

Cursor rules for stateful AI agents on Cloudflare (agents package): Durable Object state, SQL, scheduling, and WebSockets.

CursorCloudflare Workers / Hono

Convex backend rules

Cursor rules for Convex backends: schema, validated queries/mutations/actions, indexes, and auth checks.

CursorConvex

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.