agentleFS
dekozadekoza/oh-my-slop/skills/practice/django-discipline/SKILL.md

django-discipline

Skill4 starsChanged 9 months agoUnlicenseView on GitHub

Use whenever creating, modifying, or fixing any code in a Django project — a project with manage.py present or django in pyproject.toml/requirements.txt. Applies to every change in such a project, whether or not the change looks Django-specific. This is the discipline skill — for framework pattern lookups, use django instead.

The file

Show its rules and text

We split this skill into the rules it gives an agent

26 rules. About 0 are common, 10 are distinctive, and 16 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈10

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 16

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    When either is true, this checklist applies to all code you write or modify.

    line 37
  2. Distinctive:

    Follow these in order. Every item is mandatory.

    line 41
  3. Very specific:

    NEVER: write migration files by hand unless you need RunPython, RunSQL, or custom AlterField with data transforms.

    line 49
  4. Very specific:

    If you need RunPython/RunSQL: run makemigrations first to get the skeleton, then inject your custom code into the generated file.

    line 50
  5. Very specific:

    After creating migrations, verify with python manage.py migrate --check (or run migrations if the project is set up for it).

    line 51
  6. Very specific:

    If makemigrations detects no changes but you modified a model, stop and investigate — your model change may not be registered.

    line 52
  7. Distinctive:

    Run this after every code change, before any manual lint cleanup.

    line 60
  8. Distinctive:

    Only fix what ruff can't auto-fix yourself.

    line 61
  9. Very specific:

    Do NOT run isort, black, flake8, or autoflake separately — ruff subsumes all of them.

    line 62
  10. Distinctive:

    Do NOT manually remove unused imports, fix formatting, or sort imports that ruff handles.

    line 63
  11. Very specific:

    ForeignKey / OneToOne: always use .select_related("field")

    line 67
  12. Very specific:

    ManyToMany / reverse FK: always use .prefetch_related("field")

    line 68
  13. Very specific:

    M2M or reverse FK filtering: always append .distinct() to avoid duplicate rows

    line 69
  14. Very specific:

    Import from the public interface: from apps.context import Symbol

    line 84
  15. Very specific:

    Never: import from internal modules: from apps.context.models import Symbol

    line 85
  16. Distinctive:

    Before renaming any model field:

    line 90
  17. Distinctive:

    Update all references, not just the model definition

    line 92
  18. Very specific:

    Run makemigrations after the rename

    line 93
  19. Very specific:

    Use update_or_create() in test fixtures to avoid unique constraint violations

    line 97
  20. Very specific:

    Use Sequence for unique fields in tests, not django_get_or_create

    line 98
  21. Very specific:

    If using WhiteNoise: WhiteNoiseMiddleware must be second in MIDDLEWARE, directly after SecurityMiddleware.

    line 104
  22. Distinctive:

    Every item in the checklist traces to a real production bug.

    line 114
  23. Distinctive:

    If it points at production settings, tests hit production CSP, CSRF middleware, and database — causing auth failures, origin-check errors, …

    line 126
  24. Very specific:

    Add http://127.0.0.1 to CSRF_TRUSTED_ORIGINS and ALLOWED_HOSTS

    line 129
  25. Distinctive:

    Use SQLite in-memory or temp file

    line 131
  26. Very specific:

    Run pytest --collect-only to verify the correct settings module loads before committing to a full test run.

    line 134

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Django Discipline

STOP — Read This Before Writing Any Django Code

This skill is mandatory for all Django project work. If you are about to create, modify, or fix code in a Django project — stop here and follow this checklist. These rules exist because agents consistently get them wrong.

You are rationalizing if you think:

ExcuseReality
"I'll just write the migration quickly by hand"Manual migrations miss dependency chains and break migration graphs. Run makemigrations first.
"I'll fix the lint issues manually"ruff check --fix handles 90% of lint issues in one shot. Run it before manual cleanup.
"I know Django, I don't need this"That's exactly the attitude that produces N+1 queries and broken migration chains. Follow the checklist.
"This is a simple model change, no need for makemigrations"There is no simple model change. Run makemigrations. Always.
"I'll add select_related later if it's slow"You won't. The N+1 ships to production. Add it now.

Detection

You are in a Django project if:

  • manage.py exists in the project root, OR
  • pyproject.toml / requirements.txt contains django

When either is true, this checklist applies to all code you write or modify.

Django Workflow Checklist

Follow these in order. Every item is mandatory.

1. Migrations — Always Auto-Generate First

python manage.py makemigrations
  • NEVER write migration files by hand unless you need RunPython, RunSQL, or custom AlterField with data transforms.
  • If you need RunPython/RunSQL: run makemigrations first to get the skeleton, then inject your custom code into the generated file.
  • After creating migrations, verify with python manage.py migrate --check (or run migrations if the project is set up for it).
  • If makemigrations detects no changes but you modified a model, stop and investigate — your model change may not be registered.

2. Linting — Ruff Before Manual Cleanup

ruff check --fix && ruff format
  • Run this after every code change, before any manual lint cleanup.
  • Only fix what ruff can't auto-fix yourself.
  • Do NOT run isort, black, flake8, or autoflake separately — ruff subsumes all of them.
  • Do NOT manually remove unused imports, fix formatting, or sort imports that ruff handles.

3. ORM — Prevent N+1 Queries

  • ForeignKey / OneToOne: always use .select_related("field")
  • ManyToMany / reverse FK: always use .prefetch_related("field")
  • M2M or reverse FK filtering: always append .distinct() to avoid duplicate rows
  • Never access related objects in a loop without eager loading
# ❌ Bad: N+1 queries
for order in Order.objects.all():
    print(order.user.email)

# ✅ Good: single query with join
for order in Order.objects.select_related("user"):
    print(order.user.email)

4. Imports — Public API Only

  • Import from the public interface: from apps.context import Symbol
  • Never import from internal modules: from apps.context.models import Symbol
  • For circular imports: use deferred imports inside functions with # Circular import: <reason> comment, or PEP 562 lazy __getattr__ in __init__.py

5. Model Changes — Grep Before Renaming

Before renaming any model field:

  • Grep the entire codebase for the field name: views, serializers, forms, templates, test fixtures, factories, admin, services
  • Update all references, not just the model definition
  • Run makemigrations after the rename

6. Testing — Django-Specific Patterns

  • Use update_or_create() in test fixtures to avoid unique constraint violations
  • Use Sequence for unique fields in tests, not django_get_or_create
  • Prefix test-only settings with TEST_ to avoid collisions with seed/migration data
  • For E2E tests: navigate via UI clicks, not page.goto(url)

7. Settings — WhiteNoise Ordering

If using WhiteNoise: WhiteNoiseMiddleware must be second in MIDDLEWARE, directly after SecurityMiddleware.

Adjacent Tools

  • DRF: Load the drf skill for API patterns
  • django-debug-toolbar: Dev-only. Never ship to production. Verify INTERNAL_IPS is set.
  • Reference patterns (model fields, views, templates, forms, admin, auth, middleware): Load the django skill

Enforcement

Every item in the checklist traces to a real production bug. If you skip a step, you are introducing risk that has already bitten this project. Follow the checklist.

pytest Settings Module Verification

Before running pytest, verify the pytest config (pyproject.toml / pytest.ini / setup.cfg) points DJANGO_SETTINGS_MODULE at the project's dedicated test settings module — whatever the project names it — never at the production/default settings:

# pyproject.toml
[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "<project>.settings_test"  # never the production settings module

If it points at production settings, tests hit production CSP, CSRF middleware, and database — causing auth failures, origin-check errors, and data pollution. Test settings must:

  • Disable/remove CSRF middleware (CsrfViewMiddleware)
  • Add http://127.0.0.1 to CSRF_TRUSTED_ORIGINS and ALLOWED_HOSTS
  • Relax CSP directives for test environment
  • Use SQLite in-memory or temp file
  • Set DEBUG = True (allows CSRF middleware to bypass origin check)

Run pytest --collect-only to verify the correct settings module loads before committing to a full test run.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.