diff options
| author | Jeremy Dunck <jdunck@gmail.com> | 2007-06-18 16:48:27 +0000 |
|---|---|---|
| committer | Jeremy Dunck <jdunck@gmail.com> | 2007-06-18 16:48:27 +0000 |
| commit | bdcc95e5cce2754d78055f86d561ba2be92ba854 (patch) | |
| tree | 08a7e4c86244cb42fe577aec5c03a57b023822c2 /tests/modeltests/model_forms | |
| parent | 48c9f87e1f3ba9523d79c09f52c0ccc6221f08bf (diff) | |
gis: Merged revisions 4786-5490 via svnmerge from
http://code.djangoproject.com/svn/django/trunk
git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@5492 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/modeltests/model_forms')
| -rw-r--r-- | tests/modeltests/model_forms/models.py | 95 |
1 files changed, 80 insertions, 15 deletions
diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index d91f1d2d45..6ffd4d1bce 100644 --- a/tests/modeltests/model_forms/models.py +++ b/tests/modeltests/model_forms/models.py @@ -18,12 +18,18 @@ 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. """ from django.db import models +ARTICLE_STATUS = ( + (1, 'Draft'), + (2, 'Pending'), + (3, 'Live'), +) + class Category(models.Model): name = models.CharField(maxlength=20) url = models.CharField('The URL', maxlength=40) @@ -44,6 +50,7 @@ class Article(models.Model): writer = models.ForeignKey(Writer) article = models.TextField() categories = models.ManyToManyField(Category, blank=True) + status = models.IntegerField(choices=ARTICLE_STATUS, blank=True, null=True) def save(self): import datetime @@ -87,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 @@ -98,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 @@ -112,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 @@ -127,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): ... @@ -147,8 +154,8 @@ Create a couple of Writers. >>> w = Writer(name='Bob Woodward') >>> w.save() -ManyToManyFields are represented by a MultipleChoiceField, and ForeignKeys are -represented by a ChoiceField. +ManyToManyFields are represented by a MultipleChoiceField, ForeignKeys and any +fields with the 'choices' attribute are represented by a ChoiceField. >>> ArticleForm = form_for_model(Article) >>> f = ArticleForm(auto_id=False) >>> print f @@ -159,13 +166,31 @@ represented by a ChoiceField. <option value="1">Mike Royko</option> <option value="2">Bob Woodward</option> </select></td></tr> -<tr><th>Article:</th><td><textarea name="article"></textarea></td></tr> +<tr><th>Article:</th><td><textarea rows="10" cols="40" name="article"></textarea></td></tr> +<tr><th>Status:</th><td><select name="status"> +<option value="" selected="selected">---------</option> +<option value="1">Draft</option> +<option value="2">Pending</option> +<option value="3">Live</option> +</select></td></tr> <tr><th>Categories:</th><td><select multiple="multiple" name="categories"> <option value="1">Entertainment</option> <option value="2">It's a test</option> <option value="3">Third test</option> </select><br /> Hold down "Control", or "Command" on a Mac, to select more than one.</td></tr> +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 +<tr><th>Headline:</th><td><input type="text" name="headline" maxlength="50" /></td></tr> +<tr><th>Pub date:</th><td><input type="text" name="pub_date" /></td></tr> + You can pass a custom Form class to form_for_model. Make sure it's a subclass of BaseForm, not Form. >>> class CustomForm(BaseForm): @@ -199,13 +224,35 @@ current values are inserted as 'initial' data in each Field. <option value="1" selected="selected">Mike Royko</option> <option value="2">Bob Woodward</option> </select></li> -<li>Article: <textarea name="article">Hello.</textarea></li> +<li>Article: <textarea rows="10" cols="40" name="article">Hello.</textarea></li> +<li>Status: <select name="status"> +<option value="" selected="selected">---------</option> +<option value="1">Draft</option> +<option value="2">Pending</option> +<option value="3">Live</option> +</select></li> <li>Categories: <select multiple="multiple" name="categories"> <option value="1">Entertainment</option> <option value="2">It's a test</option> <option value="3">Third test</option> </select> Hold down "Control", or "Command" on a Mac, to select more than one.</li> ->>> 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() +<li>Headline: <input type="text" name="headline" value="New headline" maxlength="50" /></li> +<li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li> >>> f.is_valid() True >>> new_art = f.save() @@ -231,7 +278,13 @@ Add some categories and test the many-to-many form output. <option value="1" selected="selected">Mike Royko</option> <option value="2">Bob Woodward</option> </select></li> -<li>Article: <textarea name="article">Hello.</textarea></li> +<li>Article: <textarea rows="10" cols="40" name="article">Hello.</textarea></li> +<li>Status: <select name="status"> +<option value="" selected="selected">---------</option> +<option value="1">Draft</option> +<option value="2">Pending</option> +<option value="3">Live</option> +</select></li> <li>Categories: <select multiple="multiple" name="categories"> <option value="1" selected="selected">Entertainment</option> <option value="2">It's a test</option> @@ -309,7 +362,13 @@ the data in the database when the form is instantiated. <option value="1">Mike Royko</option> <option value="2">Bob Woodward</option> </select></li> -<li>Article: <textarea name="article"></textarea></li> +<li>Article: <textarea rows="10" cols="40" name="article"></textarea></li> +<li>Status: <select name="status"> +<option value="" selected="selected">---------</option> +<option value="1">Draft</option> +<option value="2">Pending</option> +<option value="3">Live</option> +</select></li> <li>Categories: <select multiple="multiple" name="categories"> <option value="1">Entertainment</option> <option value="2">It's a test</option> @@ -328,7 +387,13 @@ the data in the database when the form is instantiated. <option value="2">Bob Woodward</option> <option value="3">Carl Bernstein</option> </select></li> -<li>Article: <textarea name="article"></textarea></li> +<li>Article: <textarea rows="10" cols="40" name="article"></textarea></li> +<li>Status: <select name="status"> +<option value="" selected="selected">---------</option> +<option value="1">Draft</option> +<option value="2">Pending</option> +<option value="3">Live</option> +</select></li> <li>Categories: <select multiple="multiple" name="categories"> <option value="1">Entertainment</option> <option value="2">It's a test</option> @@ -459,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'} """} |
