Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

newforms: Got form_for_instance() to select initial ManyToManyField v…

…alues properly

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4261 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit b9fdf9abb841f1eda11fb4d8094372dbbbeb58cb 1 parent 0919920
Adrian Holovaty authored December 30, 2006
4  django/db/models/fields/related.py
@@ -726,6 +726,10 @@ def value_from_object(self, obj):
29  tests/modeltests/model_forms/models.py
@@ -28,7 +28,7 @@ def __str__(self):
28 28
 
29 29
 class Article(models.Model):
30 30
     headline = models.CharField(maxlength=50)
31  
-    pub_date = models.DateTimeField()
  31
+    pub_date = models.DateField()
32 32
     writer = models.ForeignKey(Writer)
33 33
     categories = models.ManyToManyField(Category, blank=True)
34 34
 
@@ -80,6 +80,8 @@ def __str__(self):
80 80
 >>> Category.objects.all()
81 81
 [<Category: Entertainment>, <Category: It's a test>]
82 82
 
  83
+If you call create() with save=False, then it will return an object that hasn't
  84
+yet been saved. In this case, it's up to you to save it.
83 85
 >>> f = CategoryForm({'name': 'Third test', 'url': 'third'})
84 86
 >>> f.errors
85 87
 {}
@@ -94,6 +96,7 @@ def __str__(self):
94 96
 >>> Category.objects.all()
95 97
 [<Category: Entertainment>, <Category: It's a test>, <Category: Third test>]
96 98
 
  99
+If you call create() with invalid data, you'll get a ValueError.
97 100
 >>> f = CategoryForm({'name': '', 'url': 'foo'})
98 101
 >>> f.errors
99 102
 {'name': [u'This field is required.']}
@@ -102,7 +105,6 @@ def __str__(self):
102 105
 Traceback (most recent call last):
103 106
 ...
104 107
 ValueError: The Category could not be created because the data didn't validate.
105  
-
106 108
 >>> f = CategoryForm({'name': '', 'url': 'foo'})
107 109
 >>> f.create()
108 110
 Traceback (most recent call last):
@@ -181,4 +183,27 @@ def __str__(self):
181 183
 >>> new_art = Article.objects.get(id=1)
182 184
 >>> new_art.headline
183 185
 'New headline'
  186
+
  187
+Add some categories and test the many-to-many form output.
  188
+>>> new_art.categories.all()
  189
+[]
  190
+>>> new_art.categories.add(Category.objects.get(name='Entertainment'))
  191
+>>> new_art.categories.all()
  192
+[<Category: Entertainment>]
  193
+>>> TestArticleForm = form_for_instance(new_art)
  194
+>>> f = TestArticleForm(auto_id=False)
  195
+>>> print f.as_ul()
  196
+<li>Headline: <input type="text" name="headline" value="New headline" maxlength="50" /></li>
  197
+<li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li>
  198
+<li>Writer: <select name="writer">
  199
+<option value="">---------</option>
  200
+<option value="1" selected="selected">Mike Royko</option>
  201
+<option value="2">Bob Woodward</option>
  202
+</select></li>
  203
+<li>Categories: <select multiple="multiple" name="categories">
  204
+<option value="1" selected="selected">Entertainment</option>
  205
+<option value="2">It&#39;s a test</option>
  206
+<option value="3">Third test</option>
  207
+</select></li>
  208
+
184 209
 """}

0 notes on commit b9fdf9a

Please sign in to comment.
Something went wrong with that request. Please try again.