{"id":14065688,"url":"https://github.com/dabapps/django-forms-dynamic","last_synced_at":"2025-10-10T22:08:50.726Z","repository":{"id":49783822,"uuid":"439139664","full_name":"dabapps/django-forms-dynamic","owner":"dabapps","description":"Resolve form field arguments dynamically when a form is instantiated","archived":false,"fork":false,"pushed_at":"2023-11-26T15:54:36.000Z","size":27,"stargazers_count":143,"open_issues_count":6,"forks_count":8,"subscribers_count":12,"default_branch":"main","last_synced_at":"2025-03-30T19:11:39.267Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dabapps.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-12-16T22:07:52.000Z","updated_at":"2025-02-18T15:37:22.000Z","dependencies_parsed_at":"2024-05-28T01:37:01.800Z","dependency_job_id":"827c5574-f999-4527-b8bd-5ddd7407113c","html_url":"https://github.com/dabapps/django-forms-dynamic","commit_stats":{"total_commits":12,"total_committers":1,"mean_commits":12.0,"dds":0.0,"last_synced_commit":"a4aaa5b727cff2d730328775a1fcf7bcf2d38788"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabapps%2Fdjango-forms-dynamic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabapps%2Fdjango-forms-dynamic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabapps%2Fdjango-forms-dynamic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabapps%2Fdjango-forms-dynamic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dabapps","download_url":"https://codeload.github.com/dabapps/django-forms-dynamic/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247543595,"owners_count":20955865,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-13T07:04:38.051Z","updated_at":"2025-10-10T22:08:45.682Z","avatar_url":"https://github.com/dabapps.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"django-forms-dynamic\n====================\n\n**Resolve form field arguments dynamically when a form is instantiated, not when it's declared.**\n\nTested against Django 3.2, 4.0, 4.1 and 4.2 on Python 3.8, 3.9, 3.10 and 3.11.\n\n![Build Status](https://github.com/dabapps/django-forms-dynamic/workflows/CI/badge.svg)\n[![pypi release](https://img.shields.io/pypi/v/django-forms-dynamic.svg)](https://pypi.python.org/pypi/django-forms-dynamic)\n\n### Installation\n\nInstall from PyPI\n\n    pip install django-forms-dynamic\n\n## Usage\n\n### Passing arguments to form fields from the view\n\nThe standard way to change a Django form's fields at runtime is override the form's `__init__` method, pass in any values you need from the view, and poke around in `self.fields`:\n\n```python\nclass SelectUserFromMyTeamForm(forms.Form):\n    user = forms.ModelChoiceField(queryset=User.objects.none())\n\n    def __init__(self, *args, **kwargs):\n        team = kwargs.pop(\"team\")\n        super().__init__(*args, **kwargs)\n        self.fields[\"user\"].queryset = User.objects.filter(team=team)\n```\n\n```python\ndef select_user_view(request):\n    form = SelectUserFromMyTeamForm(team=request.user.team)\n    return render(\"form.html\", {\"form\": form})\n```\n\nThis works, but it doesn't scale very well to more complex requirements. It also feels messy: Django forms are intended to be declarative, and this is very much procedural code.\n\nWith `django-forms-dynamic`, we can improve on this approach. We need to do two things:\n\n1. Add the `DynamicFormMixin` to your form class (before `forms.Form`).\n2. Wrap any field that needs dynamic behaviour in a `DynamicField`.\n\nThe first argument to the `DynamicField` constructor is the field _class_ that you are wrapping (eg `forms.ModelChoiceField`). All other arguments (with one special-cased exception detailed below) are passed along to the wrapped field when it is created.\n\nBut there's one very important difference: **any argument that would normally be passed to the field constructor can optionally be a _callable_**. If it is a callable, it will be called _when the form is being instantiated_ and it will be passed the form _instance_ as an argument. The value returned by this callable will then be passed into to the field's constructor as usual.\n\nBefore we see a code example, there's one further thing to note: instead of passing arbitrary arguments (like `team` in the example above) into the form's constructor in the view, we borrow a useful idiom from Django REST framework serializers and instead pass a _single_ argument called `context`, which is a dictionary that can contain any values you need from the view. This is attached to the form as `form.context`.\n\nHere's how the code looks now:\n\n```python\nfrom dynamic_forms import DynamicField, DynamicFormMixin\n\n\nclass SelectUserFromMyTeamForm(DynamicFormMixin, forms.Form):\n    user = DynamicField(\n        forms.ModelChoiceField,\n        queryset=lambda form: User.objects.filter(team=form.context[\"team\"]),\n    )\n```\n\n```python\ndef select_user_view(request):\n    form = SelectUserFromMyTeamForm(context={\"team\": request.user.team})\n    return render(\"form.html\", {\"form\": form})\n```\n\nThis is much nicer!\n\n## Truly dynamic forms with XHR\n\nBut let's go further. Once we have access to the `form`, we can make forms truly dynamic by configuring fields based on the values of _other_ fields. This doesn't really make sense in the standard Django request/response approach, but it _does_ make sense when we bring JavaScript into the equation. A form can be loaded from the server multiple times (or in multiple pieces) by making XHR requests from JavaScript code running in the browser.\n\nImplementing this \"from scratch\" in JavaScript is left as an exercise for the reader. Instead, let's look at how you might do this using some modern \"low JavaScript\" frameworks.\n\n### [HTMX](https://htmx.org/)\n\nTo illustrate the pattern we're going to use one of the examples from the HTMX documentation: \"Cascading Selects\". This is where the options available in one `\u003cselect\u003e` depend on the value chosen in another `\u003cselect\u003e`. See [the HTMX docs page](https://htmx.org/examples/value-select/) for full details and a working example.\n\nHow would we implement the backend of this using `django-forms-dynamic`?\n\nFirst, let's have a look at the form:\n\n```python\nclass MakeAndModelForm(DynamicFormMixin, forms.Form):\n    MAKE_CHOICES = [\n        (\"audi\", \"Audi\"),\n        (\"toyota\", \"Toyota\"),\n        (\"bmw\", \"BMW\"),\n    ]\n\n    MODEL_CHOICES = {\n        \"audi\": [\n            (\"a1\", \"A1\"),\n            (\"a3\", \"A3\"),\n            (\"a6\", \"A6\"),\n        ],\n        \"toyota\": [\n            (\"landcruiser\", \"Landcruiser\"),\n            (\"tacoma\", \"Tacoma\"),\n            (\"yaris\", \"Yaris\"),\n        ],\n        \"bmw\": [\n            (\"325i\", \"325i\"),\n            (\"325ix\", \"325ix\"),\n            (\"x5\", \"X5\"),\n        ],\n    }\n\n    make = forms.ChoiceField(\n        choices=MAKE_CHOICES,\n        initial=\"audi\",\n    )\n    model = DynamicField(\n        forms.ChoiceField,\n        choices=lambda form: form.MODEL_CHOICES[form[\"make\"].value()],\n    )\n```\n\nThe key bit is right at the bottom. We're using a lambda function to load the choices for the `model` field based on the currently selected value of the `make` field. When the form is first shown to the user, `form[\"make\"].value()` will be `\"audi\"`: the `initial` value supplied to the `make` field. After the form is bound, `form[\"make\"].value()` will return whatever the user selected in the `make` dropdown.\n\nHTMX tends to encourage a pattern of splitting your UI into lots of small endpoints that return fragments of HTML. So we need two views: one to return the entire form on first page load, and one to return _just_ the HTML for the `model` field. The latter will be loaded whenever the `make` field changes, and will return the available `models` for the chosen `make`.\n\nHere are the two views:\n\n```python\ndef htmx_form(request):\n    form = MakeAndModelForm()\n    return render(request, \"htmx.html\", {\"form\": form})\n\n\ndef htmx_models(request):\n    form = MakeAndModelForm(request.GET)\n    return HttpResponse(form[\"model\"])\n```\n\nRemember that the string representation of `form[\"model\"]` (the bound field) is the HTML for the `\u003cselect\u003e` element, so we can return this directly in the `HttpResponse`.\n\nThese can be wired up to URLs like this:\n\n```python\nurlpatterns = [\n    path(\"htmx-form/\", htmx_form),\n    path(\"htmx-form/models/\", htmx_models),\n]\n```\n\nAnd finally, we need a template. We're using [django-widget-tweaks](https://github.com/jazzband/django-widget-tweaks) to add the necessary `hx-` attributes to the `make` field right in the template.\n\n```django\n{% load widget_tweaks %}\n\u003c!DOCTYPE html\u003e\n\n\u003chtml\u003e\n  \u003chead\u003e\n    \u003cscript src=\"https://unpkg.com/htmx.org@1.6.1\"\u003e\u003c/script\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    \u003cform method=\"POST\"\u003e\n      \u003ch3\u003ePick a make/model\u003c/h3\u003e\n      {% csrf_token %}\n      \u003cdiv\u003e\n        {{ form.make.label_tag }}\n        {% render_field form.make hx-get=\"/htmx-form/models/\" hx-target=\"#id_model\" %}\n      \u003c/div\u003e\n      \u003cdiv\u003e\n        {{ form.model.label_tag }}\n        {{ form.model }}\n      \u003c/div\u003e\n    \u003c/form\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n### [Unpoly](https://unpoly.com/)\n\nLet's build exactly the same thing with Unpoly. Unpoly favours a slightly different philosophy: rather than having the backend returning HTML fragments, it tends to prefer the server to return full HTML pages with every XHR request, and \"plucks out\" the relevant element(s) and inserts them into the DOM, replacing the old ones.\n\nWhen it comes to forms, Unpoly uses a special attribute `[up-validate]` to mark fields which, when changed, should trigger the form to be submitted and re-validated. [The docs for `[up-validate]`](https://unpoly.com/input-up-validate) also describe it as \"a great way to partially update a form when one field depends on the value of another field\", so this is what we'll use to implement our cascading selects.\n\nThe form is exactly the same as the HTMX example above. But this time, we only need one view!\n\n```python\ndef unpoly_form(request):\n    form = MakeAndModelForm(request.POST or None)\n    return render(request, \"unpoly.html\", {\"form\": form})\n```\n\n```python\nurlpatterns = [\n    path(\"unpoly-form/\", unpoly_form),\n]\n```\n\nAnd the template is even more simple:\n\n```django\n{% load widget_tweaks %}\n\u003c!DOCTYPE html\u003e\n\n\u003chtml\u003e\n  \u003chead\u003e\n    \u003cscript src=\"https://unpkg.com/unpoly@2.5.0/unpoly.min.js\"\u003e\u003c/script\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    \u003cform method=\"POST\"\u003e\n      \u003ch3\u003ePick a make/model\u003c/h3\u003e\n      {% csrf_token %}\n      \u003cdiv\u003e\n        {{ form.make.label_tag }}\n        {% render_field form.make up-validate=\"form\" %}\n      \u003c/div\u003e\n      \u003cdiv\u003e\n        {{ form.model.label_tag }}\n        {{ form.model }}\n      \u003c/div\u003e\n    \u003c/form\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n## The `include` argument\n\nThere's one more feature we might need: what if we want to remove a field from the form entirely unless another field has a particular value? To accomplish this, the `DynamicField` constructor takes one special argument that isn't passed along to the constructor of the wrapped field: `include`. Just like any other argument, this can be a callable that is passed the form instance, and it should return a boolean: `True` if the field should be included in the form, `False` otherwise. Here's an example:\n\n```python\nclass CancellationReasonForm(DynamicFormMixin, forms.Form):\n    CANCELLATION_REASONS = [\n        (\"too-expensive\", \"Too expensive\"),\n        (\"too-boring\", \"Too boring\"),\n        (\"other\", \"Other\"),\n    ]\n\n    cancellation_reason = forms.ChoiceField(choices=CANCELLATION_REASONS)\n    reason_if_other = DynamicField(\n        forms.CharField,\n        include=lambda form: form[\"cancellation_reason\"].value() == \"other\",\n    )\n```\n\n## Known gotcha: callable arguments\n\nOne thing that might catch you out: if the object you're passing in to your form field's constructor is _already_ a callable, you will need to wrap it in another callable that takes the `form` argument and returns the _actual_ callable you want to pass to the field.\n\nThis is most likely to crop up when you're passing a custom widget class, because classes are callable:\n\n```python\nclass CancellationReasonForm(DynamicFormMixin, forms.Form):\n    ...  # other fields\n\n    reason_if_other = DynamicField(\n        forms.CharField,\n        include=lambda form: form[\"cancellation_reason\"].value() == \"other\",\n        widget=lambda _: forms.TextArea,\n    )\n```\n\n## Why the awkward name?\n\nBecause `django-dynamic-forms` was already taken.\n\n## Code of conduct\n\nFor guidelines regarding the code of conduct when contributing to this repository please review [https://www.dabapps.com/open-source/code-of-conduct/](https://www.dabapps.com/open-source/code-of-conduct/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdabapps%2Fdjango-forms-dynamic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdabapps%2Fdjango-forms-dynamic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdabapps%2Fdjango-forms-dynamic/lists"}