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.

More: all Django rules · all Cursor rules

Related rules

Django CLAUDE.md

CLAUDE.md for Django 5 projects with DRF, pytest-django and PostgreSQL.

Claude Code (CLAUDE.md)Django

Django Windsurf rules

Windsurf rules for Django 5.2 LTS projects: apps, models and migrations, ORM performance, forms, and tests.

WindsurfDjango

Cloudflare Agents SDK rules

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

CursorCloudflare Workers / Hono

Fastify 5 API rules

Cursor rules for Fastify 5 APIs in TypeScript: plugins and encapsulation, JSON schema validation, hooks, and inject tests.

CursorNode.js / TypeScript

Go gRPC service rules

Cursor rules for gRPC services in Go with protobuf, buf for codegen and linting, interceptors, and status errors.

CursorGo

GraphQL server (Apollo) rules

Cursor rules for schema-first GraphQL servers with Apollo Server, codegen types, DataLoader, and safe errors.

CursorGraphQL

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.