Skip to content

project rulesBackend and APIspythondjango

Django Rules

Project rules for Django: small focused apps, logic in models and services, efficient ORM queries, safe migrations and pytest-django tests.

npx workspacetemplates add django-rules --tool claude-code

Writes 1 file to your project root

CLAUDE.mdadd to your file23 lines · 1.3 KBFile
# Project rules: Django

## Layout
- One app per domain concept, each with `models.py`, `views.py`, `urls.py`, `tests/`. Add `services.py` for multi-step business logic.
- Settings are split into base, local and production modules. Read secrets from the environment; never commit `SECRET_KEY`.

## Models and views
- Put rules about one record on the model or its manager and queryset. Keep views thin: parse input, call a service, return a response.
- Always set `related_name`, `__str__`, `Meta.ordering` where order matters, and database constraints for invariants.
- Use `get_object_or_404`. Validate input with forms or serializers, never by hand.

## ORM
- Use `select_related` for foreign keys and `prefetch_related` for reverse and many-to-many. Never query inside a loop or template.
- Use `F()` expressions and `transaction.atomic()` for updates that race. Use `bulk_create` for large inserts.

## Migrations
- Generate with `makemigrations`, read the file, and commit it. Never edit a migration that has shipped. Split schema and data changes, and add `RunPython` with a reverse function.

## Tests
- pytest-django with factories. Use the test client for views and test permissions explicitly.

## Before finishing
Run `python manage.py check`, `makemigrations --check --dry-run`, `ruff check .` and `pytest`.

What it does

Django gives you a lot of structure, and agents still tend to pile logic into views or run queries inside loops. These rules point the work back to the places Django expects it.

They also keep migrations and settings safe, since those are the two areas where a careless edit hurts a live database or leaks a secret.

Example

you › Show each author with their book count on the dashboard

agent › The agent annotates the queryset with Count, avoids a per-row query, keeps the view short, and adds a test that asserts the number of queries.

Questions

Does it work with Django REST framework?
Yes. The layout and ORM rules apply unchanged; add serializer conventions to the file if your team has them.
Why forbid editing shipped migrations?
Other environments already applied them, so a change makes databases drift apart. A new migration is always the safe path.
  • ASP.NET Core Web API Rules

    Project rulesdotnet

    Free
  • Flask Rules

    Project rulespython

    Free
  • Go Service Rules

    Project rulesgo

    Free
  • GraphQL API Rules

    Project rulesgraphql

    Free