Home › Django › Django Windsurf rules

Django Windsurf rules

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

WindsurfDjango

How to use

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

.windsurf/rules/django.mdDownload  
---
trigger: glob
globs: **/*.py,**/templates/**
---

# Django 5.2

- One Django app per domain area. Business logic in `services.py` functions or model methods, not in views or templates.
- Models: explicit `related_name`, `on_delete`, `Meta.ordering` only when needed, `constraints` (`UniqueConstraint`, `CheckConstraint`) instead of app-level checks. Use `db_default` for DB-side defaults.
- After model changes run `python manage.py makemigrations <app>` and review the file. Data migrations use `RunPython` with the historical model (`apps.get_model`).
- ORM performance: `select_related`/`prefetch_related` for related data, `only()`/`values()` for large reads, `bulk_create`/`update()` for batch writes, `F()` expressions for atomic updates. Avoid queries inside loops and templates.
- Class-based or function views, matching the app; protect them with `LoginRequiredMixin`/`@login_required` and permission checks.
- Forms (`ModelForm`) validate all user input; templates keep autoescape on and include `{% csrf_token %}`.
- Settings from env (django-environ or similar); `DEBUG=False` and `ALLOWED_HOSTS` in production.
- Async views only when the whole call chain is async (`aget`, `afilter`).

## Done when
`python manage.py check --deploy` (for settings changes), `makemigrations --check`, `ruff check .`, and the test suite (`pytest` or `manage.py test`) pass.

Related rules

Web accessibility (WCAG 2.2) rules

Windsurf rules for building accessible web UIs to WCAG 2.2 AA: semantics, keyboard, focus, contrast, and testing.

WindsurfAccessibility

Django CLAUDE.md

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

Claude Code (CLAUDE.md)Django

Django REST Framework rules

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

CursorDjango

Documentation writing rules

Windsurf rules for writing and updating READMEs, docstrings, ADRs, and changelogs alongside code changes.

WindsurfAgent workflows

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.