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.1.0.tar.gz (8.4 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.1.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_dict_form-0.1.0.tar.gz
  • Upload date:
  • Size: 8.4 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.1.0.tar.gz
Algorithm Hash digest
SHA256 0f757c6e144baaa4989626b219232bdd11c362a9bcbe588a8481a61ea5d6ced1
MD5 85a82b6b4a2334df65eddca67b82a998
BLAKE2b-256 292d14d5f44bd67a4231dfe27aa70b001754ea1754ab6857331b82873bc2b01e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_dict_form-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0776423b80a7e457b8b556b22318b2c49ce1e6f2ac513c45bbcff7cd1ac6ec01
MD5 7774fea77fa0c9ca04ead25660325f40
BLAKE2b-256 f163b4906a2349a8f7051de5c40c6bfb3d14c6f0c3a6711056496fc5495a3cfc

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