# Django Review

> Django review checklist — what to check in Django code, by severity. A reference for writing and reviewing it; in a workflow run the review step records each problem as a finding and the engine decides what blocks.

- Skill: `keyvaluesoftwaresystems/django-review` (Agent Skill)
- Install (CLI): `npx get-skill add keyvaluesoftwaresystems/django-review`
- Raw SKILL.md: https://api.skillmd.com/api/skills/keyvaluesoftwaresystems/django-review/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Productivity
- Author: keyvaluesoftwaresystems (https://skillmd.com/u/keyvaluesoftwaresystems)
- Updated: 2026-11-11
- Page: https://skillmd.com/skills/keyvaluesoftwaresystems/django-review

---


# Django review checklist

Use this when writing or reviewing Django code. The severities say how serious a problem is; they are guidance for a finding's severity, not a verdict.

## Review Priorities

### CRITICAL — Security

- **SQL Injection**: Raw SQL with f-strings or `%` formatting — use `%s` parameters or ORM
- **`mark_safe` on user input**: Never without explicit `escape()` first
- **CSRF exemption without reason**: `@csrf_exempt` on non-webhook views
- **`DEBUG = True` in production settings**: Leaks full stack traces
- **Hardcoded `SECRET_KEY`**: Must come from environment variable
- **Missing `permission_classes` on DRF views**: Defaults to global — verify intent
- **`eval()`/`exec()` on user input**: Immediate block
- **File upload without extension/size validation**: Path traversal risk

### CRITICAL — ORM Correctness

- **N+1 queries in loops**: Accessing related objects without `select_related`/`prefetch_related`
  ```python
  # Bad
  for order in Order.objects.all():
      print(order.user.email)  # N+1

  # Good
  for order in Order.objects.select_related('user').all():
      print(order.user.email)
  ```
- **Missing `atomic()` for multi-step writes**: Use `transaction.atomic()` for any sequence of DB writes
- **`bulk_create` without `update_conflicts`**: Silent data loss on duplicate keys
- **`get()` without `DoesNotExist` handling**: Unhandled exception risk
- **Queryset used after `delete()`**: Stale queryset reference

### CRITICAL — Migration Safety

- **Model change without migration**: Run `python manage.py makemigrations --check`
- **Backward-incompatible column drop**: Must be done in two deployments (nullable first)
- **`RunPython` without `reverse_code`**: Migration cannot be reversed
- **`atomic = False` without justification**: Leaves DB in partial state on failure

### HIGH — DRF Patterns

- **Serializer without explicit `fields`**: `fields = '__all__'` exposes all columns including sensitive ones
- **No pagination on list endpoints**: Unbounded queries can return millions of rows
- **Missing `read_only_fields`**: Auto-generated fields (id, created_at) editable by API
- **`perform_create` not used**: Injecting user context should happen in `perform_create`, not `validate`
- **No throttling on auth endpoints**: Login/registration open to brute force
- **Nested writable serializers without `update()`**: Default update silently ignores nested data

### HIGH — Performance

- **Queryset evaluated in template context**: Use `.values()` or pass list; avoid lazy evaluation in templates
- **Missing `db_index` on FK/filter fields**: Full table scan on filtered queries
- **Synchronous external API call in view**: Blocks the request thread — offload to Celery
- **`len(queryset)` instead of `.count()`**: Forces full fetch
- **`exists()` not used for existence checks**: `if queryset:` fetches objects unnecessarily

  ```python
  # Bad
  if Product.objects.filter(sku=sku):
      ...

  # Good
  if Product.objects.filter(sku=sku).exists():
      ...
  ```

### HIGH — Code Quality

- **Business logic in views or serializers**: Move to `services.py`
- **Signal logic that belongs in a service**: Signals make flow hard to trace — use explicitly
- **Mutable default in model field**: `default=[]` or `default={}` — use `default=list`
- **`save()` called without `update_fields`**: Overwrites all columns — risk of clobbering concurrent writes

  ```python
  # Bad
  user.last_active = now()
  user.save()

  # Good
  user.last_active = now()
  user.save(update_fields=['last_active'])
  ```

### MEDIUM — Best Practices

- **`str(queryset)` or slicing for debug**: Use Django shell, not production code
- **Accessing `request.user` in serializer `validate()`**: Pass via context, not direct access
- **`print()` instead of `logger`**: Use `logging.getLogger(__name__)`
- **Missing `related_name`**: Reverse accessors like `user_set` are confusing
- **`blank=True` without `null=True` on non-string fields**: DB stores empty string for non-string types
- **Hardcoded URLs**: Use `reverse()` or `reverse_lazy()`
- **Missing `__str__` on models**: Django admin and logging are broken without it
- **App not using `AppConfig.ready()`**: Signal receivers not connected properly

### MEDIUM — Testing Gaps

- **No test for permission boundary**: Verify unauthorized access returns 403/401
- **`force_authenticate` instead of proper token**: Tests skip auth logic entirely
- **Missing `@pytest.mark.django_db`**: Tests silently hit no DB
- **Factory not used**: Raw `Model.objects.create()` in tests is fragile

## Diagnostic Commands

```bash
python manage.py check               # Django system check
python manage.py makemigrations --check  # Detect missing migrations
ruff check .                         # Fast linter
mypy . --ignore-missing-imports      # Type checking
bandit -r . -ll                      # Security scan (medium+)
pytest --cov=apps --cov-report=term-missing -q  # Tests + coverage
```

## Framework-Specific Checks

- **Migrations**: Every model change must have a migration. Two-phase for column removal.
- **DRF**: All public endpoints need explicit `permission_classes`. Pagination on all list views.
- **Celery**: Tasks must be idempotent. Use `bind=True` + `self.retry()` for transient failures.
- **Django Admin**: Never expose sensitive fields. Use `readonly_fields` for auto-generated data.
- **Signals**: Prefer explicit service calls. If signals are used, register in `AppConfig.ready()`.

## Reference

For Django architecture patterns and ORM examples, see `skill: django-patterns`.
For security configuration checklists, see `skill: django-security`.
For testing patterns and fixtures, see `skill: django-tdd`.

---

Review with the mindset: "Would this code safely serve 10,000 concurrent users without data loss, security breach, or a 3am pager alert?"

