From 433659139596a75eab03940ea2029970de6ee287 Mon Sep 17 00:00:00 2001
From: Joseph Kocherhans
Date: Tue, 15 May 2007 03:37:41 +0000
Subject: newforms-admin: Merged to [5243]. There are 3 failing tests in
regressiontests.serializers_regress.tests.SerializerTests, but they fail in
trunk also.
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@5244 bcc190cf-cafb-0310-a4f2-bffc1f526a37
---
tests/modeltests/model_forms/models.py | 44 ++++--
tests/regressiontests/forms/formsets.py | 32 ++---
tests/regressiontests/forms/regressions.py | 14 ++
tests/regressiontests/forms/tests.py | 158 ++++++++++++++++-----
.../regressiontests/serializers_regress/models.py | 10 ++
tests/regressiontests/serializers_regress/tests.py | 6 +
.../regressiontests/test_client_regress/models.py | 28 ++++
7 files changed, 232 insertions(+), 60 deletions(-)
(limited to 'tests')
diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py
index a23529b566..6ffd4d1bce 100644
--- a/tests/modeltests/model_forms/models.py
+++ b/tests/modeltests/model_forms/models.py
@@ -18,7 +18,7 @@ other Form, with one additional method: save(). The save()
method updates the model instance. It also takes a commit=True parameter.
The function django.newforms.save_instance() takes a bound form instance and a
-model instance and saves the form's clean_data into the instance. It also takes
+model instance and saves the form's cleaned_data into the instance. It also takes
a commit=True parameter.
"""
@@ -94,7 +94,7 @@ __test__ = {'API_TESTS': """
>>> f = CategoryForm({'name': 'Entertainment', 'url': 'entertainment'})
>>> f.is_valid()
True
->>> f.clean_data
+>>> f.cleaned_data
{'url': u'entertainment', 'name': u'Entertainment'}
>>> obj = f.save()
>>> obj
@@ -105,7 +105,7 @@ True
>>> f = CategoryForm({'name': "It's a test", 'url': 'test'})
>>> f.is_valid()
True
->>> f.clean_data
+>>> f.cleaned_data
{'url': u'test', 'name': u"It's a test"}
>>> obj = f.save()
>>> obj
@@ -119,7 +119,7 @@ save() on the resulting model instance.
>>> f = CategoryForm({'name': 'Third test', 'url': 'third'})
>>> f.is_valid()
True
->>> f.clean_data
+>>> f.cleaned_data
{'url': u'third', 'name': u'Third test'}
>>> obj = f.save(commit=False)
>>> obj
@@ -134,10 +134,10 @@ If you call save() with invalid data, you'll get a ValueError.
>>> f = CategoryForm({'name': '', 'url': 'foo'})
>>> f.errors
{'name': [u'This field is required.']}
->>> f.clean_data
+>>> f.cleaned_data
Traceback (most recent call last):
...
-AttributeError: 'CategoryForm' object has no attribute 'clean_data'
+AttributeError: 'CategoryForm' object has no attribute 'cleaned_data'
>>> f.save()
Traceback (most recent call last):
...
@@ -179,6 +179,18 @@ fields with the 'choices' attribute are represented by a ChoiceField.
Hold down "Control", or "Command" on a Mac, to select more than one.
+You can restrict a form to a subset of the complete list of fields
+by providing a 'fields' argument. If you try to save a
+model created with such a form, you need to ensure that the fields
+that are _not_ on the form have default values, or are allowed to have
+a value of None. If a field isn't specified on a form, the object created
+from the form can't provide a value for that field!
+>>> PartialArticleForm = form_for_model(Article, fields=('headline','pub_date'))
+>>> f = PartialArticleForm(auto_id=False)
+>>> print f
+
Headline:
+
Pub date:
+
You can pass a custom Form class to form_for_model. Make sure it's a
subclass of BaseForm, not Form.
>>> class CustomForm(BaseForm):
@@ -224,7 +236,23 @@ current values are inserted as 'initial' data in each Field.
Hold down "Control", or "Command" on a Mac, to select more than one.
->>> f = TestArticleForm({'headline': u'New headline', 'pub_date': u'1988-01-04', 'writer': u'1', 'article': 'Hello.'})
+>>> f = TestArticleForm({'headline': u'Test headline', 'pub_date': u'1984-02-06', 'writer': u'1', 'article': 'Hello.'})
+>>> f.is_valid()
+True
+>>> test_art = f.save()
+>>> test_art.id
+1
+>>> test_art = Article.objects.get(id=1)
+>>> test_art.headline
+'Test headline'
+
+You can create a form over a subset of the available fields
+by specifying a 'fields' argument to form_for_instance.
+>>> PartialArticleForm = form_for_instance(art, fields=('headline','pub_date'))
+>>> f = PartialArticleForm({'headline': u'New headline', 'pub_date': u'1988-01-04'}, auto_id=False)
+>>> print f.as_ul()
+
Headline:
+
Pub date:
>>> f.is_valid()
True
>>> new_art = f.save()
@@ -496,6 +524,6 @@ ValidationError: [u'Select a valid choice. 10 is not one of the available choice
>>> f = PhoneNumberForm({'phone': '(312) 555-1212', 'description': 'Assistance'})
>>> f.is_valid()
True
->>> f.clean_data
+>>> f.cleaned_data
{'phone': u'312-555-1212', 'description': u'Assistance'}
"""}
diff --git a/tests/regressiontests/forms/formsets.py b/tests/regressiontests/forms/formsets.py
index 8d0e3b8d7c..96aa86a9b2 100644
--- a/tests/regressiontests/forms/formsets.py
+++ b/tests/regressiontests/forms/formsets.py
@@ -38,14 +38,14 @@ the COUNT field appropriately.
... }
We treat FormSet pretty much like we would treat a normal Form. FormSet has an
-is_valid method, and a clean_data or errors attribute depending on whether all
-the forms passed validation. However, unlike a Form instance, clean_data and
+is_valid method, and a cleaned_data or errors attribute depending on whether all
+the forms passed validation. However, unlike a Form instance, cleaned_data and
errors will be a list of dicts rather than just a single dict.
>>> formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
>>> formset.is_valid()
True
->>> formset.clean_data
+>>> formset.cleaned_data
[{'votes': 100, 'choice': u'Calexico'}]
@@ -64,12 +64,12 @@ False
>>> formset.errors
[{'votes': [u'This field is required.']}]
-Like a Form instance, clean_data won't exist if the formset wasn't validated.
+Like a Form instance, cleaned_data won't exist if the formset wasn't validated.
->>> formset.clean_data
+>>> formset.cleaned_data
Traceback (most recent call last):
...
-AttributeError: 'ChoiceFormSet' object has no attribute 'clean_data'
+AttributeError: 'ChoiceFormSet' object has no attribute 'cleaned_data'
We can also prefill a FormSet with existing data by providing an ``initial``
@@ -99,7 +99,7 @@ Let's simulate what would happen if we submitted this form.
>>> formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
>>> formset.is_valid()
True
->>> formset.clean_data
+>>> formset.cleaned_data
[{'votes': 100, 'choice': u'Calexico'}]
But the second form was blank! Shouldn't we get some errors? No. If we display
@@ -176,7 +176,7 @@ number of forms to be completed.
>>> formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
>>> formset.is_valid()
True
->>> formset.clean_data
+>>> formset.cleaned_data
[]
@@ -195,7 +195,7 @@ We can just fill out one of the forms.
>>> formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
>>> formset.is_valid()
True
->>> formset.clean_data
+>>> formset.cleaned_data
[{'votes': 100, 'choice': u'Calexico'}]
@@ -262,7 +262,7 @@ False
We can easily add deletion ability to a FormSet with an agrument to
formset_for_form. This will add a boolean field to each form instance. When
that boolean field is True, the cleaned data will be in formset.deleted_data
-rather than formset.clean_data
+rather than formset.cleaned_data
>>> ChoiceFormSet = formset_for_form(Choice, deletable=True)
@@ -299,7 +299,7 @@ To delete something, we just need to set that form's special delete field to
>>> formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
>>> formset.is_valid()
True
->>> formset.clean_data
+>>> formset.cleaned_data
[{'votes': 100, 'DELETE': False, 'choice': u'Calexico'}]
>>> formset.deleted_data
[{'votes': 900, 'DELETE': True, 'choice': u'Fergie'}]
@@ -308,7 +308,7 @@ True
We can also add ordering ability to a FormSet with an agrument to
formset_for_form. This will add a integer field to each form instance. When
-form validation succeeds, formset.clean_data will have the data in the correct
+form validation succeeds, formset.cleaned_data will have the data in the correct
order specified by the ordering fields. If a number is duplicated in the set
of ordering fields, for instance form 0 and form 3 are both marked as 1, then
the form index used as a secondary ordering criteria. In order to put
@@ -346,8 +346,8 @@ something at the front of the list, you'd need to set it's order to 0.
>>> formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
>>> formset.is_valid()
True
->>> for clean_data in formset.clean_data:
-... print clean_data
+>>> for cleaned_data in formset.cleaned_data:
+... print cleaned_data
{'votes': 500, 'ORDER': 0, 'choice': u'The Decemberists'}
{'votes': 100, 'ORDER': 1, 'choice': u'Calexico'}
{'votes': 900, 'ORDER': 2, 'choice': u'Fergie'}
@@ -408,8 +408,8 @@ Let's delete Fergie, and put The Decemberists ahead of Calexico.
>>> formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
>>> formset.is_valid()
True
->>> for clean_data in formset.clean_data:
-... print clean_data
+>>> for cleaned_data in formset.cleaned_data:
+... print cleaned_data
{'votes': 500, 'DELETE': False, 'ORDER': 0, 'choice': u'The Decemberists'}
{'votes': 100, 'DELETE': False, 'ORDER': 1, 'choice': u'Calexico'}
>>> formset.deleted_data
diff --git a/tests/regressiontests/forms/regressions.py b/tests/regressiontests/forms/regressions.py
index 5daabc03af..5fe057b5d8 100644
--- a/tests/regressiontests/forms/regressions.py
+++ b/tests/regressiontests/forms/regressions.py
@@ -34,4 +34,18 @@ Unicode decoding problems...
>>> f = SomeForm()
>>> f.as_p()
u'
\n\n\n\n
'
+
+#######################
+# Miscellaneous Tests #
+#######################
+
+There once was a problem with Form fields called "data". Let's make sure that
+doesn't come back.
+>>> class DataForm(Form):
+... data = CharField(max_length=10)
+>>> f = DataForm({'data': 'xyzzy'})
+>>> f.is_valid()
+True
+>>> f.cleaned_data
+{'data': u'xyzzy'}
"""
diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py
index f15034ec66..943cddd1d7 100644
--- a/tests/regressiontests/forms/tests.py
+++ b/tests/regressiontests/forms/tests.py
@@ -1775,7 +1775,7 @@ True
u''
>>> p.errors.as_text()
u''
->>> p.clean_data
+>>> p.cleaned_data
{'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)}
>>> print p['first_name']
@@ -1811,10 +1811,10 @@ True
{'first_name': [u'This field is required.'], 'last_name': [u'This field is required.'], 'birthday': [u'This field is required.']}
>>> p.is_valid()
False
->>> p.clean_data
+>>> p.cleaned_data
Traceback (most recent call last):
...
-AttributeError: 'Person' object has no attribute 'clean_data'
+AttributeError: 'Person' object has no attribute 'cleaned_data'
>>> print p
This field is required.
This field is required.
@@ -1845,10 +1845,10 @@ False
{}
>>> p.is_valid()
False
->>> p.clean_data
+>>> p.cleaned_data
Traceback (most recent call last):
...
-AttributeError: 'Person' object has no attribute 'clean_data'
+AttributeError: 'Person' object has no attribute 'cleaned_data'
>>> print p
@@ -1887,10 +1887,10 @@ u'
first_name
This field is re
* This field is required.
* birthday
* This field is required.
->>> p.clean_data
+>>> p.cleaned_data
Traceback (most recent call last):
...
-AttributeError: 'Person' object has no attribute 'clean_data'
+AttributeError: 'Person' object has no attribute 'cleaned_data'
>>> p['first_name'].errors
[u'This field is required.']
>>> p['first_name'].errors.as_ul()
@@ -1906,17 +1906,45 @@ u'* This field is required.'
>>> print p['birthday']
-clean_data will always *only* contain a key for fields defined in the
+cleaned_data will always *only* contain a key for fields defined in the
Form, even if you pass extra data when you define the Form. In this
example, we pass a bunch of extra fields to the form constructor,
-but clean_data contains only the form's fields.
+but cleaned_data contains only the form's fields.
>>> data = {'first_name': u'John', 'last_name': u'Lennon', 'birthday': u'1940-10-9', 'extra1': 'hello', 'extra2': 'hello'}
>>> p = Person(data)
>>> p.is_valid()
True
->>> p.clean_data
+>>> p.cleaned_data
{'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)}
+cleaned_data will include a key and value for *all* fields defined in the Form,
+even if the Form's data didn't include a value for fields that are not
+required. In this example, the data dictionary doesn't include a value for the
+"nick_name" field, but cleaned_data includes it. For CharFields, it's set to the
+empty string.
+>>> class OptionalPersonForm(Form):
+... first_name = CharField()
+... last_name = CharField()
+... nick_name = CharField(required=False)
+>>> data = {'first_name': u'John', 'last_name': u'Lennon'}
+>>> f = OptionalPersonForm(data)
+>>> f.is_valid()
+True
+>>> f.cleaned_data
+{'nick_name': u'', 'first_name': u'John', 'last_name': u'Lennon'}
+
+For DateFields, it's set to None.
+>>> class OptionalPersonForm(Form):
+... first_name = CharField()
+... last_name = CharField()
+... birth_date = DateField(required=False)
+>>> data = {'first_name': u'John', 'last_name': u'Lennon'}
+>>> f = OptionalPersonForm(data)
+>>> f.is_valid()
+True
+>>> f.cleaned_data
+{'birth_date': None, 'first_name': u'John', 'last_name': u'Lennon'}
+
"auto_id" tells the Form to add an "id" attribute to each form element.
If it's a string that contains '%s', Django will use that as a format string
into which the field's name will be inserted. It will also put a