Skip to main content

django-treeform

from django import forms
from django_treeform import TreeForm, SequenceNode, Sequence

class CharacteristicForm(TreeForm):
    id = forms.IntegerField()
    name = forms.CharField()
    rating = forms.CharField()

class PersonForm(TreeForm):
    id = forms.IntegerField()
    name = forms.CharField()
    phone = forms.CharField()
    Charecteristics = SequenceNode(CharacteristicForm)

params = [{
    "id": 1,
    "name": "abc",
    "phone": "12345",
    "Charecteristics": [
        {
            "id": 1,
            "name": "Good Looking",
            "rating": "Average",
        },
        {
            "id": 2,
            "name": "Smart",
            "rating": "Excellent",
        }
    ]
},
{
    "id": 2,
    "name": "abc",
    "phone": "12345",
    "Charecteristics": [
        {
            "id": 1,
            "name": "Good Looking",
            "rating": "Average",
        },
        {
            "id": 2,
            "name": "Smart",
            "rating": "Excellent",
        }
    ]
}]
form = Sequence(PersonForm)(params)
print(form.is_valid()) # => True

more nested forms

from django import forms
from django_treeform import SequenceNode, TreeForm

class ItemForm(forms.Form):
    name = forms.CharField()


class NestedForm(TreeForm):
    class a(TreeForm):
        class b(TreeForm):
            class c(TreeForm):
                class d(TreeForm):
                    class e(TreeForm):
                        items = SequenceNode(ItemForm)

params = {
    "a": {"b": {"c": {"d": {"e": {"items": [{"name": "A"}, {"name": "B"}]}}}}}
}
formlike = NestedForm(params)

print(formlike.is_valid()) # => True
expected = {'a': {'b': {'c': {'d': {'e': {'items': [{'name': 'A'}, {'name': 'B'}]}}}}}}
assert formlike.cleaned_data == expected
assert formlike.errors == {'a': {'b': {'c': {'d': {'e': {'items': [{}, {}]}}}}}}

custom validation

class PointForm(forms.Form):
    x = forms.IntegerField()
    y = forms.IntegerField()


class PointPairForm(TreeForm):
    left = Node(PointForm)
    right = Node(PointForm)

    def clean(self):
        if self.has_error():
            return
        if self.cleaned_data["left"]["x"] < self.cleaned_data["right"]["x"]:
            raise forms.ValidationError("oops")

params = {"left": {"x": 10, "y": 20}, "right": {"x": 20, "y": "20"}}
formlike = PointPairForm(params)
print(formlike.is_valid() # => False
print(formlike.errors) # => {"left": {}, "right": {}, "__all__": ["oops"]}

Metadata

Release files for django-treeform 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-treeform 0.1
File Size Uploaded Provenance Actions
django-treeform-0.1.tar.gz 4.8 kB Without provenance Details for django-treeform-0.1.tar.gz

Release files / django-treeform-0.1.tar.gz

Download URL django-treeform-0.1.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
eea1ad5bc5129058616a76912380862e6790bfd78e4c3ab497ca2da27000c7ea
BLAKE2b-256 checksum
How to use checksums
8700c9672e08ce65481de4903c20cdcd6f2d77a15b803e544f1a5077bbf4d261
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for django-treeform 0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page