How dabapps/django-forms-dynamic works
Project Type and Purpose
• This is an open‑source Python library (“django‑forms‑dynamic”) that plugs into the Django framework to let form field arguments be resolved at runtime.
• It supplies two core abstractions—DynamicFormMixin and DynamicField—to replace procedural form initialization logic with declarative, lazy‑evaluation behavior.
Main Components
a. Library Core (dynamic_forms package)
• DynamicFormMixin (mixin that overrides form __init__)
• DynamicField (wrapper around Django form fields that accepts callables)
b. Django Framework
• forms.Form base class
• request/response cycle, template rendering
c. User Application Code (example usage)
• View functions (htmx_form, htmx_models, unpoly_form)
• URL conf (urlpatterns)
• Templates (htmx.html, unpoly.html)
d. Runtime Mechanics
• Form instantiation with context dict
• Callable resolution of field arguments (λ form → value)
• Field inclusion logic (include parameter)
e. Supporting Infrastructure
• Tests suite (tests/test_forms.py, tests/settings.py)
• CI pipelines (.github/workflows)
• Packaging (setup.py, setup.cfg, PyPI release workflow)
Relationships and Data Flow
• View → “DynamicFormMixin.__init__” (passes context)
• __init__ → “DynamicField” (evaluates any callable args against form instance)
• DynamicField → underlying Django Field → “Form.fields” collection
• Form → “Template Renderer” → “HTML response” → Back to Browser
• Optional XHR branches: Browser sends XHR on field change → “AJAX View endpoint” → generates partial form or field HTML → reinjected into page.
• CI pipelines → test runner → PyPI publish
Architectural Patterns and Principles
• Mixin pattern to extend Django forms without modifying core code
• Wrapper/decorator around field classes for lazy argument evaluation
• Use of callables for deferred computation (inversion of control)
• Separation of concerns between form definition, HTTP handling, and UI fragment updates (HTMX/Unpoly)