-
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
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
django-treeform-0.1.tar.gz
(4.8 kB
view details)
File details
Details for the file django-treeform-0.1.tar.gz
.
File metadata
- Download URL: django-treeform-0.1.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | eea1ad5bc5129058616a76912380862e6790bfd78e4c3ab497ca2da27000c7ea |
|
MD5 | 43456bdcb885a5ec6bd2b34b9ec09774 |
|
BLAKE2b-256 | 8700c9672e08ce65481de4903c20cdcd6f2d77a15b803e544f1a5077bbf4d261 |