Modelli ricchi, view snelle, migrazioni revisionate, niente SQL grezzo senza motivo.
Constitution — Django monolith
Version 1.0.0 · Ratified 2026-11-10
These principles hold for every spec, plan and task of the project. A spec may add rules; it never weakens these.
-
Fat models. Business rules live on models, model methods and custom managers or querysets. A rule that touches one model is a method on that model, not code repeated in views or forms.
-
Thin views. Views parse the request with a form or serializer, call one model or service method and render the result. A view longer than about 30 lines or with business conditions fails the review.
-
Migrations reviewed. Every model change ships its generated migration in the same pull request, and
makemigrations --checkruns in CI. Data migrations useRunPythonwith a reverse function, and migrations that lock large tables are flagged in the plan. -
No raw SQL without a reason. Queries use the ORM.
raw(),extra()andcursor.executeneed a justification in the plan (a measured query the ORM cannot express) and always pass parameters, never formatted strings. -
No N+1 queries. List views use
select_relatedandprefetch_relatedfor the relations they render. Tests assert query counts withassertNumQuerieson every list endpoint. -
Apps by domain. Each Django app owns one business area with its models, admin, urls and tests. Apps import each other through public functions, not by reaching into another app’s internals.
-
Settings from the environment. Secrets and per-environment values come from environment variables;
DEBUGis off andALLOWED_HOSTSis explicit outside development.manage.py check --deploypasses before a release. -
Tests with the test client. Every view has a test with Django’s test client or pytest-django covering success, permission denied and invalid input. Factories (factory_boy) build data; shared fixtures files are avoided.