Management Kit¶
The Management Kit is the part of django-mindoff that edits your project on disk. Runtime components handle requests and queue jobs. This layer handles the repository itself.
It creates and updates apps, APIs, models, settings, and routes so the project keeps a consistent structure as it grows.
For guided command usage, see:
- Developer Guide - Installation & Configuration
- Developer Guide - App and Model Setup
- Developer Guide - API Development
Architectural Philosophy¶
The core idea is straightforward:
- Separate command orchestration from file mutation.
- The command layer should stay readable and easy to extend.
- The mutation layer should stay deterministic and safe to run repeatedly.
The Orchestrator-Mutator Split¶
| Component | Responsibility | Examples |
|---|---|---|
| Orchestrators | Decide what should happen. Manage CLI arguments, prompts, and flow. | init.py, create.py, delete.py |
| Mutators | Decide how files change. Perform idempotent writes, patching, and template-based generation. | _create_api.py, _create_app.py |
Command Discovery & Extension¶
The CLI is module-driven. mindoff.py discovers manager modules from django_mindoff.components.managers instead of using one large command switch.
- Participation: A module is included only if it exposes
register_subcommand(subparsers). - Discovery rules: Package modules are skipped, and
init/nukeare explicitly excluded by the generatedmindoff.pydiscovery loop. - Practical result: You can add new management capability by adding a manager module, not by rewriting central CLI logic.
Interactive vs Direct Usage¶
You can use the toolkit in two ways:
- Interactive orchestration (
create,delete) for guided workflows. - Direct command invocation (
createapp,createapi,createmodel,create_model_field,deleteapp) for experienced users, comfortable with exact CLI inputs.
Direct Command Reference¶
These commands can be called directly via python mindoff.py <command> ....
| Command | Purpose | Required args | Optional args | Notes |
|---|---|---|---|---|
createapp |
Create one or more Django apps under apps/ |
app_names (nargs="+") |
None | Uses Django manage.py startapp under the hood. |
createapi |
Create API class + router + URL + tests | api_path (<app>/<api_name>) |
--url <pattern...> |
Default URL becomes <api_name>/ if omitted. |
createmodel |
Create model + serializer stubs | model_path (<app>/<ModelName>) |
None | Model name normalized to PascalCase + Model. |
create_model_field |
Add a ForeignKey field to existing model |
model_path, field_name |
--to <app>/<ModelName> |
--to is required at runtime for FK creation. |
deleteapp |
Delete one or more apps and unwind wiring | app_names (nargs="+") |
None | Removes app dir and related settings.py/urls.py entries. |
Examples:
python mindoff.py createapp billingpython mindoff.py createmodel billing/Invoicepython mindoff.py create_model_field billing/Invoice customer --to crm/Customerpython mindoff.py createapi billing/create_invoice --url create/ create/<int:id>/
Key Lifecycles¶
1. The Initialization Pipeline¶
init is a full bootstrap flow, not just a thin wrapper around startproject.
- Environment: Creates a virtual environment and installs managed dependencies.
- Scaffolding: Generates the Django project and patches
settings.pyandurls.py. - Resources: Adds support files such as
mindoff.py,pytest.ini,.gitignore,.env, HTML templates, andresponses.csv. - Version Control: Initializes git to capture the generated baseline.
Default Package Set Installed by init¶
init.py installs dependencies from pyproject.toml into the new .venv.
The list below is auto-synced from code at docs build time:
django>=5.0djangorestframework>=3.15.0,<4.0python-decouple>=3.8,<4.0django-ratelimit>=4.1.0,<5.0pytest>=8.0,<9.0pytest-django>=4.8.0,<5.0model-bakery>=1.20.0,<2.0typeguard>=4.0,<5.0polars>=1.39.0,<2.0pandas>=2.2.0,<3.0sqlalchemy>=2.0.0,<3.0orjson>=3.10.0,<4.0pyarrow>=17.0.0,<20.0.0dramatiq>=1.17.0,<2.0redis>=5.0.0,<6.0.0
django-mindoff is installed after the base list as a separate install step.
Optional Packages
optional_packages exists in the flow, but is currently empty unless extended. Optional packages are open to be extended for modders.
Why Virtual Environment and Git Are Enforced¶
The default init behavior is opinionated on purpose:
- Virtual environment from day 1: ensures dependency isolation and reproducible runtime behavior.
- Git initialization from day 1: captures a clean baseline commit right after scaffolding, making later changes reviewable and rollback-friendly.
Together, this supports the intended outcome: production-minded API projects with stable environments, traceable changes, and predictable deployments.
Reliability: Built on Django CLI Under the Hood¶
Management Kit does not reimplement framework scaffolding primitives. It delegates core creation tasks to Django commands:
- Project creation uses
django-admin startproject config . - App creation uses
python manage.py startapp <app> <target_dir>
This keeps long-term behavior aligned with Django internals and reduces drift risk across framework upgrades.
URL and Template Setup Performed by init¶
init configures first-run project UX and API entry wiring:
- Updates
config/urls.pyto include a root homepage route:path("", TemplateView.as_view(template_name="index.html")). - Updates
config/urls.pyto include Mindoff routes:path("mindoff/", include(mindoff_urls)). - Updates
settings.pytemplate DIRS to includetemplates/ - Copies default templates (
index.html,404.html) into projecttemplates/
Operationally, this gives a custom homepage immediately and provides a project-level 404.html template that Django can render when DEBUG=False.
2. The Creation Flow¶
create.py works as an interactive dispatcher. It collects user input, validates naming/selection rules, and forwards execution to mutator commands via subprocess calls.
For direct command usage and arguments, see Direct Command Reference.
3. The Delete App Flow¶
Delete is also split into orchestrator + mutator stages:
delete.pyruns an interactive app selection flow (deletecommand).- It forwards selected apps to
deleteappvia subprocess. _delete_app.py(DjangoAppDeleter) performs per-app deletion with confirmation.- After filesystem deletion, it unwinds project wiring by removing the app from
INSTALLED_APPS, removing the versioned include from projecturls.py, and cleaning empty parent namespace directories when applicable.
This makes app removal structured and predictable instead of a manual folder delete.
4. The Nuke Flow¶
nuke.py handles project-level teardown with explicit safety controls:
- Prompts for scope: Basic removes core generated artifacts while preserving defaults like
.gitand.venv; Full removes all listed artifacts, including.gitand.venv. - Builds a concrete target list from known artifact paths.
- Supports
--dry-runpreview before deleting anything. - Requires explicit confirmation before destructive execution.
- Deletes targets and prints deleted/skipped summary.
nuke is intentionally explicit and interactive because it operates at repository scope.
Specialized Mutators¶
Mutators are designed around idempotency. Running an operation twice should not leave the project in a broken state.
App & API Generation¶
_create_app.py: Standardizesapps/<app>/, replaces defaulttests.pywith structured test folders, and wires the app intoINSTALLED_APPSand root version routing._create_api.py: Generates API class, router wiring, URL entries, and related tests from templates, and blocks accidental overwrite of existing V1 API definitions.
Model & Field Engineering¶
_create_model.py: Enforces naming conventions (PascalCase +Model) and applies table naming (tbl_<app>_<model_base>)._create_modelfield.py: Adds foreign key fields only, using managed markers inside model classes instead of blind append operations, so generated code lands in a stable location.
The Safety Model¶
This layer assumes file edits are high-impact, so safeguards are part of normal execution rather than optional extras.
- File Guardians: Top-level
run()methods in managers/mutators are wrapped withmo_helper_kit.file_guardian. - Existence Checks: Write targets are validated before mutation.
- Controlled Deletion:
nukesafety behavior is detailed in The Nuke Flow.
How file_guardian Works Here¶
file_guardian is transaction-style rollback protection for file mutations inside a manager run:
- It monkey-patches write surfaces (
open,os.makedirs,Path.write_text,Path.write_bytes,Path.mkdir). - During execution it tracks newly created files/directories and modified files (with temporary backups in a rollback directory).
- If execution succeeds, rollback backups are discarded.
- If execution fails, created files are deleted, modified files are restored from backup, created directories are removed in reverse order, and patched functions are restored.
Practical boundary:
- It protects Python-side file operations executed inside the wrapped process.
- It does not roll back side effects produced inside external subprocesses (for example commands run by
subprocess.run).
Troubleshooting the Kit¶
Most issues come from registration gaps or manual edits inside generated areas.
- Registration: Ensure new managers expose
register_subcommand. - Discovery behavior: If a command does not appear in generated
mindoff.py, verify discovery exclusions documented in Command Discovery & Extension. - Wiring: If a route is unreachable, verify generated entries in
urls.py. - Markers: If field generation fails, check whether the model insertion marker was removed.
- Direct command errors: For direct usage, verify argument formats exactly (
<app>/<ModelName>,<app>/<api_name>,--to <app>/<ModelName>).