summaryrefslogtreecommitdiff
path: root/tests/modeltests/model_forms
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2006-12-27 05:23:21 +0000
committerAdrian Holovaty <adrian@holovaty.com>2006-12-27 05:23:21 +0000
commit6a75c8a52ef01c680677c13cefb08a9ccbe38562 (patch)
tree1c5ec29689a99021546d39a8c32945c72349b22e /tests/modeltests/model_forms
parentd853278253b5efa60a691ae9b83fb6352d2a91f0 (diff)
newforms: Implemented formfield() for database ForeignKey class and added unit tests
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4247 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/modeltests/model_forms')
-rw-r--r--tests/modeltests/model_forms/models.py21
1 files changed, 20 insertions, 1 deletions
diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py
index da10e215a6..566dad42ea 100644
--- a/tests/modeltests/model_forms/models.py
+++ b/tests/modeltests/model_forms/models.py
@@ -20,9 +20,16 @@ class Category(models.Model):
def __str__(self):
return self.name
+class Writer(models.Model):
+ name = models.CharField(maxlength=50)
+
+ def __str__(self):
+ return self.name
+
class Article(models.Model):
headline = models.CharField(maxlength=50)
pub_date = models.DateTimeField()
+ writer = models.ForeignKey(Writer)
categories = models.ManyToManyField(Category)
def __str__(self):
@@ -101,12 +108,24 @@ Traceback (most recent call last):
...
ValueError: The Category could not be created because the data didn't validate.
-ManyToManyFields are represented by a MultipleChoiceField.
+Create a couple of Writers.
+>>> w = Writer(name='Mike Royko')
+>>> w.save()
+>>> w = Writer(name='Bob Woodward')
+>>> w.save()
+
+ManyToManyFields are represented by a MultipleChoiceField, and ForeignKeys are
+represented by a ChoiceField.
>>> ArticleForm = form_for_model(Article)
>>> f = ArticleForm(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>
+<tr><th>Writer:</th><td><select name="writer">
+<option value="" selected="selected">---------</option>
+<option value="1">Mike Royko</option>
+<option value="2">Bob Woodward</option>
+</select></td></tr>
<tr><th>Categories:</th><td><select multiple="multiple" name="categories">
<option value="1">Entertainment</option>
<option value="2">It&#39;s a test</option>