Skip to main content

-

Project description

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"]}

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

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

Files for django-treeform, version 0.1
Filename, size File type Python version Upload date Hashes
Filename, size django-treeform-0.1.tar.gz (4.8 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page