Resolve form field arguments dynamically when a form is instantiated

Overview

django-forms-dynamic

Resolve form field arguments dynamically when a form is instantiated, not when it's declared.

Tested against Django 2.2, 3.2 and 4.0 on Python 3.6, 3.7, 3.8, 3.9 and 3.10

Build Status pypi release

Installation

Install from PyPI

pip install django-forms-dynamic

Usage

Passing arguments to form fields from the view

The 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:

class SelectUserFromMyTeamForm(forms.Form):
    user = forms.ModelChoiceField(queryset=User.objects.none())

    def __init__(self, *args, **kwargs):
        team = kwargs.pop("team")
        super().__init__(*args, **kwargs)
        self.fields["user"].queryset = User.objects.filter(team=team)
def select_user_view(request):
    form = SelectUserFromMyTeamForm(team=request.user.team)
    return render("form.html", {"form": form})

This 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.

With django-forms-dynamic, we can improve on this approach. We need to do two things:

  1. Add the DynamicFormMixin to your form class (before forms.Form).
  2. Wrap any field that needs dynamic behaviour in a DynamicField.

The 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.

But 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.

Before 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.

Here's how the code looks now:

from dynamic_forms import DynamicField, DynamicFormMixin


class SelectUserFromMyTeamForm(DynamicFormMixin, forms.Form):
    user = DynamicField(
        forms.ModelChoiceField,
        queryset=lambda form: User.objects.filter(team=form.context["team"]),
    )
def select_user_view(request):
    form = SelectUserFromMyTeamForm(context={"team": request.user.team})
    return render("form.html", {"form": form})

This is much nicer!

Truly dynamic forms with XHR

But 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.

Implementing 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.

HTMX

To 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 <select> depend on the value chosen in another <select>. See the HTMX docs page for full details and a working example.

How would we implement the backend of this using django-forms-dynamic?

First, let's have a look at the form:

class MakeAndModelForm(DynamicFormMixin, forms.Form):
    MAKE_CHOICES = [
        ("audi", "Audi"),
        ("toyota", "Toyota"),
        ("bmw", "BMW"),
    ]

    MODEL_CHOICES = {
        "audi": [
            ("a1", "A1"),
            ("a3", "A3"),
            ("a6", "A6"),
        ],
        "toyota": [
            ("landcruiser", "Landcruiser"),
            ("tacoma", "Tacoma"),
            ("yaris", "Yaris"),
        ],
        "bmw": [
            ("325i", "325i"),
            ("325ix", "325ix"),
            ("x5", "X5"),
        ],
    }

    make = forms.ChoiceField(
        choices=MAKE_CHOICES,
        initial="audi",
    )
    model = DynamicField(
        forms.ChoiceField,
        choices=lambda form: form.MODEL_CHOICES[form["make"].value()],
    )

The 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.

HTMX 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.

Here are the two views:

def htmx_form(request):
    form = MakeAndModelForm()
    return render(request, "htmx.html", {"form": form})


def htmx_models(request):
    form = MakeAndModelForm(request.GET)
    return HttpResponse(form["model"])

Remember that the string representation of form["model"] (the bound field) is the HTML for the <select> element, so we can return this directly in the HttpResponse.

These can be wired up to URLs like this:

urlpatterns = [
    path("htmx-form/", htmx_form),
    path("htmx-form/models/", htmx_models),
]

And finally, we need a template. We're using django-widget-tweaks to add the necessary hx- attributes to the make field right in the template.

{% load widget_tweaks %}
<!DOCTYPE html>

<html>
  <head>
    <script src="https://unpkg.com/[email protected]"></script>
  </head>
  <body>
    <form method="POST">
      <h3>Pick a make/model</h3>
      {% csrf_token %}
      <div>
        {{ form.make.label_tag }}
        {% render_field form.make hx-get="/htmx-form/models/" hx-target="#id_model" %}
      </div>
      <div>
        {{ form.model.label_tag }}
        {{ form.model }}
      </div>
    </form>
  </body>
</html>

Unpoly

Let'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.

When 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] 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.

The form is exactly the same as the HTMX example above. But this time, we only need one view!

def unpoly_form(request):
    form = MakeAndModelForm(request.POST or None)
    return render(request, "unpoly.html", {"form": form})
urlpatterns = [
    path("unpoly-form/", unpoly_form),
]

And the template is even more simple:

{% load widget_tweaks %}
<!DOCTYPE html>

<html>
  <head>
    <script src="https://unpkg.com/[email protected]/unpoly.min.js"></script>
  </head>
  <body>
    <form method="POST">
      <h3>Pick a make/model</h3>
      {% csrf_token %}
      <div>
        {{ form.make.label_tag }}
        {% render_field form.make up-validate="form" %}
      </div>
      <div>
        {{ form.model.label_tag }}
        {{ form.model }}
      </div>
    </form>
  </body>
</html>

The include argument

There'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:

class CancellationReasonForm(DynamicFormMixin, forms.Form):
    CANCELLATION_REASONS = [
        ("too-expensive", "Too expensive"),
        ("too-boring", "Too boring"),
        ("other", "Other"),
    ]

    cancellation_reason = forms.ChoiceField(choices=CANCELLATION_REASONS)
    reason_if_other = DynamicField(
        forms.CharField,
        include=lambda form: form["cancellation_reason"].value() == "other",
    )

Known gotcha: callable arguments

One 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.

This is most likely to crop up when you're passing a custom widget class, because classes are callable:

class CancellationReasonForm(DynamicFormMixin, forms.Form):
    ...  # other fields

    reason_if_other = DynamicField(
        forms.CharField,
        include=lambda form: form["cancellation_reason"].value() == "other",
        widget=lambda _: forms.TextArea,
    )

Why the awkward name?

Because django-dynamic-forms was already taken.

Code of conduct

For guidelines regarding the code of conduct when contributing to this repository please review https://www.dabapps.com/open-source/code-of-conduct/

You might also like...
A drop-in replacement for django's ImageField that provides a flexible, intuitive and easily-extensible interface for quickly creating new images from the one assigned to the field.

django-versatileimagefield A drop-in replacement for django's ImageField that provides a flexible, intuitive and easily-extensible interface for creat

A pickled object field for Django

django-picklefield About django-picklefield provides an implementation of a pickled object field. Such fields can contain any picklable objects. The i

Full control of form rendering in the templates.

django-floppyforms Full control of form rendering in the templates. Authors: Gregor Müllegger and many many contributors Original creator: Bruno Renié

Twitter Bootstrap for Django Form

Django bootstrap form Twitter Bootstrap for Django Form. A simple Django template tag to work with Bootstrap Installation Install django-bootstrap-for

Full control of form rendering in the templates.

django-floppyforms Full control of form rendering in the templates. Authors: Gregor Müllegger and many many contributors Original creator: Bruno Renié

Twitter Bootstrap for Django Form - A simple Django template tag to work with Bootstrap

Twitter Bootstrap for Django Form - A simple Django template tag to work with Bootstrap

This is a sample Django Form.

Sample FORM Installation guide Clone repository git clone https://github.com/Ritabratadas343/SampleForm.git cd to repository. Create a virtualenv by f

Basic Form Web Development using Python, Django and CSS

thebookrain Basic Form Web Development using Python, Django and CSS This is a basic project that contains two forms - borrow and donate. The form data

MAC address Model Field & Form Field for Django apps

django-macaddress MAC Address model and form fields for Django We use netaddr to parse and validate the MAC address. The tests aren't complete yet. Pa

Tweak the form field rendering in templates, not in python-level form definitions. CSS classes and HTML attributes can be altered.

django-widget-tweaks Tweak the form field rendering in templates, not in python-level form definitions. Altering CSS classes and HTML attributes is su

Tweak the form field rendering in templates, not in python-level form definitions. CSS classes and HTML attributes can be altered.

django-widget-tweaks Tweak the form field rendering in templates, not in python-level form definitions. Altering CSS classes and HTML attributes is su

:package: :fire: Python project management. Manage packages: convert between formats, lock, install, resolve, isolate, test, build graph, show outdated, audit. Manage venvs, build package, bump version.
:package: :fire: Python project management. Manage packages: convert between formats, lock, install, resolve, isolate, test, build graph, show outdated, audit. Manage venvs, build package, bump version.

THE PROJECT IS ARCHIVED Forks: https://github.com/orsinium/forks DepHell -- project management for Python. Why it is better than all other tools: Form

Django query profiler - one profiler to rule them all.  Shows queries, detects N+1 and gives recommendations on how to resolve them
Django query profiler - one profiler to rule them all. Shows queries, detects N+1 and gives recommendations on how to resolve them

Django Query Profiler This is a query profiler for Django applications, for helping developers answer the question "My Django code/page/API is slow, H

Django query profiler - one profiler to rule them all.  Shows queries, detects N+1 and gives recommendations on how to resolve them
Django query profiler - one profiler to rule them all. Shows queries, detects N+1 and gives recommendations on how to resolve them

Django Query Profiler This is a query profiler for Django applications, for helping developers answer the question "My Django code/page/API is slow, H

Automatically resolve RidderMaster based on TensorFlow & OpenCV
Automatically resolve RidderMaster based on TensorFlow & OpenCV

AutoRiddleMaster Automatically resolve RidderMaster based on TensorFlow & OpenCV 基于 TensorFlow 和 OpenCV 实现的全自动化解御迷士小马谜题 Demo How to use Deploy the ser

A raw implementation of the nearest insertion algorithm to resolve TSP problems in a TXT format.

TSP-Nearest-Insertion A raw implementation of the nearest insertion algorithm to resolve TSP problems in a TXT format. Instructions Load a txt file wi

A Project to resolve hostname and receive IP

hostname-resolver A Project to resolve hostname and receive IP Installation git clone https://github.com/ihapiw/hostname-resolver.git Head into the ho

A django model and form field for normalised phone numbers using python-phonenumbers

django-phonenumber-field A Django library which interfaces with python-phonenumbers to validate, pretty print and convert phone numbers. python-phonen

A django model and form field for normalised phone numbers using python-phonenumbers

django-phonenumber-field A Django library which interfaces with python-phonenumbers to validate, pretty print and convert phone numbers. python-phonen

Releases(1.0.0)
Owner
DabApps
We design and build bespoke web and mobile applications that help our clients succeed
DabApps
Drf-stripe-subscription - An out-of-box Django REST framework solution for payment and subscription management using Stripe

Drf-stripe-subscription - An out-of-box Django REST framework solution for payment and subscription management using Stripe

Oscar Y Chen 68 Jan 07, 2023
Simple tagging for django

django-taggit This is a Jazzband project. By contributing you agree to abide by the Contributor Code of Conduct and follow the guidelines. django-tagg

Jazzband 3k Jan 02, 2023
A app for managing lessons with Django

Course Notes A app for managing lessons with Django Some Ideas

Motahhar.Mokfi 6 Jan 28, 2022
Add Chart.js visualizations to your Django admin using a mixin class

django-admincharts Add Chart.js visualizations to your Django admin using a mixin class. Example from django.contrib import admin from .models import

Dropseed 22 Nov 22, 2022
Coltrane - A simple content site framework that harnesses the power of Django without the hassle.

coltrane A simple content site framework that harnesses the power of Django without the hassle. Features Can be a standalone static site or added to I

Adam Hill 58 Jan 02, 2023
Projeto Crud Django and Mongo

Projeto-Crud_Django_and_Mongo Configuração para rodar o projeto Download Project

Samuel Fernandes Oliveira 2 Jan 24, 2022
Loguru is an exceeding easy way to do logging in Python

Django Easy Logging Easy Django logging with Loguru Loguru is an exceeding easy way to do logging in Python. django-easy-logging makes it exceedingly

Neutron Sync 8 Oct 17, 2022
I managed to attach the Django Framework to my Telegram Bot and set a webhook

I managed to attach the Django Framework to my Telegram Bot and set a webhook. I've been developing it from 10th of November 2021 and I want to have a basic working prototype.

Valentyn Vovchak 2 Sep 08, 2022
Send logs to RabbitMQ from Python/Django.

python-logging-rabbitmq Logging handler to ships logs to RabbitMQ. Compatible with Django. Installation Install using pip. pip install python_logging_

Alberto Menendez Romero 38 Nov 17, 2022
This is a simple Todo web application built Django (back-end) and React JS (front-end)

Django REST Todo app This is a simple Todo web application built with Django (back-end) and React JS (front-end). The project enables you to systemati

Maxim Mukhin 5 May 06, 2022
demo project for django channels tutorial

django_channels_chat_official_tutorial demo project for django channels tutorial code from tutorial page: https://channels.readthedocs.io/en/stable/tu

lightsong 1 Oct 22, 2021
An automatic django's update checker and MS teams notifier

Django Update Checker This is small script for checking any new updates/bugfixes/security fixes released in django News & Events and sending correspon

prinzpiuz 4 Sep 26, 2022
RedisTimeSeries python client

redistimeseries-py Deprecation notice As of redis-py 4.0.0 this library is deprecated. It's features have been merged into redis-py. Please either ins

98 Dec 08, 2022
Automatically deletes old file for FileField and ImageField. It also deletes files on models instance deletion.

Django Cleanup Features The django-cleanup app automatically deletes files for FileField, ImageField and subclasses. When a FileField's value is chang

Ilya Shalyapin 838 Dec 30, 2022
Social Media Network Focuses On Data Security And Being Community Driven Web App

privalise Social Media Network Focuses On Data Security And Being Community Driven Web App The Main Idea: We`ve seen social media web apps that focuse

Privalise 8 Jun 25, 2021
This is a repository for a web application developed with Django, built with Crowdbotics

assignment_32558 This is a repository for a web application developed with Django, built with Crowdbotics Table of Contents Project Structure Features

Crowdbotics 1 Dec 29, 2021
REST API with Django and SQLite3

REST API with Django and SQLite3

Luis Quiñones Requelme 1 Nov 07, 2021
Create a netflix-like service using Django, React.js, & More.

Create a netflix-like service using Django. Learn advanced Django techniques to achieve amazing results like never before.

Coding For Entrepreneurs 67 Dec 08, 2022
Django CacheMiddleware has a multi-threading issue with pylibmc

django-pylibmc-bug Django CacheMiddleware has a multi-threading issue with pylibmc. CacheMiddleware shares a thread-unsafe cache object with many thre

Iuri de Silvio 1 Oct 19, 2022
Django Serverless Cron - Run cron jobs easily in a serverless environment

Django Serverless Cron - Run cron jobs easily in a serverless environment

Paul Onteri 41 Dec 16, 2022