Skip to main content

Is a lightweight extension for Django Forms that adds serialization

Project description

django-dict-form

Lightweight Django Forms extension. Adds as_dict() — serializes form definitions to Python dicts / JSON, making forms easier to integrate with REST APIs, SPA frontends (React/Vue), dynamic UI generators, and low-code systems.

Extends standard Django form rendering (as_p, as_table, as_ul) with machine-readable field metadata, validation rules, and widget configuration.

When is this convenient?

Write adapters to UI/UX components once — forms auto-generate on frontend. Backend edits propagate automatically. No manual frontend form duplication.

Features

  • Serialize Django forms to Python dictionaries
  • Useful for dynamic frontend rendering
  • Simplifies API-driven form generation
  • Lightweight and framework-friendly
  • Compatible with standard Django Forms

Install

pip install django-dict-form

Quick Start

from django_dict_form.form import DictForm
from django import forms

class ContactForm(DictForm):
    name = forms.CharField(max_length=100)
    email = forms.EmailField()
    message = forms.CharField(widget=forms.Textarea)

form = ContactForm()
data = form.as_dict()

Output:

{
  "name": {
    "label": null,
    "required": true,
    "disabled": false,
    "help_text": "",
    "initial": null,
    "show_hidden_initial": false,
    "label_suffix": null,
    "widget": {
      "name": "TextInput",
      "type": "text",
      "is_hidden": false,
      "required": true,
      "choices": [],
      "attrs": {"maxlength": "100"}
    }
  }
}

Mixin Usage

Use RenderableDictFormMixin to add as_dict() to any existing form class:

from django_dict_form.form import RenderableDictFormMixin
from django import forms

class MyForm(forms.Form, RenderableDictFormMixin):
    field = forms.CharField()

as_dict() Output Schema

Each field produces:

Key Type Description
label str | null Field label
label_suffix str | null Label suffix
required bool Whether field is required
disabled bool Whether field is disabled
help_text str Help text
initial any Default value
show_hidden_initial bool Show hidden initial widget
widget dict Widget metadata (see below)

Widget dict:

Key Type Description
name str Widget class name (e.g. TextInput)
type str | null HTML input type (e.g. text, email, number)
is_hidden bool Whether widget is hidden
required bool Widget-level required
choices list[{value, name}] Options for select widgets
attrs dict HTML attributes (e.g. maxlength, min, max)

Supported Fields

Field Widget Notes
CharField TextInput maxlength, minlength in attrs
IntegerField NumberInput min, max in attrs
FloatField NumberInput step: any in attrs
EmailField EmailInput maxlength: 320 default
URLField URLInput
BooleanField CheckboxInput
ChoiceField Select choices serialized
DateField DateInput
TimeField TimeInput
DateTimeField DateTimeInput
DurationField TextInput
RegexField TextInput
UUIDField TextInput

Custom widgets supported — pass any widget instance, choices and attrs extracted automatically.

Requirements

  • Python >= 3.11.15
  • Django >= 3.2

License

MIT — Gleb Uvarov

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_dict_form-0.11.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_dict_form-0.11.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file django_dict_form-0.11.0.tar.gz.

File metadata

  • Download URL: django_dict_form-0.11.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for django_dict_form-0.11.0.tar.gz
Algorithm Hash digest
SHA256 5f8b3de6302b53217e88d6f40ebb47431b883007771c724933ea3cb587e8422a
MD5 81d3d566b69293ad4c59d67616e8a4bc
BLAKE2b-256 002caf0cec55e7e9f3c83ba57ada00c1e5e999067cef1cb28cac91041ccef8fc

See more details on using hashes here.

File details

Details for the file django_dict_form-0.11.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_dict_form-0.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 38b5b9882923066579ca5f8ab64c593ba57337f14f489457cc679e7a7061f9ce
MD5 8b88207973164da2e02fabe061b669a2
BLAKE2b-256 2d81b3a6d67040e0679853db9db4ca76ccc517d94837b4755ba093167247a553

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page