Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3268 -- Changed default model formfields to use a select widge…

…t when the

field has a choices attribute. Based on a patch from mrmachine.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@5119 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit d9cc22b3e3b66f468c0df0d84ce54521ded79836 1 parent b09bdd2
Malcolm Tredinnick authored April 28, 2007
2  django/db/models/fields/__init__.py
@@ -344,6 +344,8 @@ def _get_choices(self):
344 344
     def formfield(self, form_class=forms.CharField, **kwargs):
345 345
         "Returns a django.newforms.Field instance for this database Field."
346 346
         defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
  347
+        if self.choices:
  348
+            defaults['widget'] = forms.Select(choices=self.get_choices())
347 349
         defaults.update(kwargs)
348 350
         return form_class(**defaults)
349 351
 
41  tests/modeltests/model_forms/models.py
@@ -24,6 +24,12 @@
24 24
 
25 25
 from django.db import models
26 26
 
  27
+ARTICLE_STATUS = (
  28
+    (1, 'Draft'),
  29
+    (2, 'Pending'),
  30
+    (3, 'Live'),
  31
+)
  32
+
27 33
 class Category(models.Model):
28 34
     name = models.CharField(maxlength=20)
29 35
     url = models.CharField('The URL', maxlength=40)
@@ -44,6 +50,7 @@ class Article(models.Model):
44 50
     writer = models.ForeignKey(Writer)
45 51
     article = models.TextField()
46 52
     categories = models.ManyToManyField(Category, blank=True)
  53
+    status = models.IntegerField(choices=ARTICLE_STATUS, blank=True, null=True)
47 54
 
48 55
     def save(self):
49 56
         import datetime
@@ -147,8 +154,8 @@ def __str__(self):
147 154
 >>> w = Writer(name='Bob Woodward')
148 155
 >>> w.save()
149 156
 
150  
-ManyToManyFields are represented by a MultipleChoiceField, and ForeignKeys are
151  
-represented by a ChoiceField.
  157
+ManyToManyFields are represented by a MultipleChoiceField, ForeignKeys and any
  158
+fields with the 'choices' attribute are represented by a ChoiceField.
152 159
 >>> ArticleForm = form_for_model(Article)
153 160
 >>> f = ArticleForm(auto_id=False)
154 161
 >>> print f
@@ -160,6 +167,12 @@ def __str__(self):
160 167
 <option value="2">Bob Woodward</option>
161 168
 </select></td></tr>
162 169
 <tr><th>Article:</th><td><textarea rows="10" cols="40" name="article"></textarea></td></tr>
  170
+<tr><th>Status:</th><td><select name="status">
  171
+<option value="" selected="selected">---------</option>
  172
+<option value="1">Draft</option>
  173
+<option value="2">Pending</option>
  174
+<option value="3">Live</option>
  175
+</select></td></tr>
163 176
 <tr><th>Categories:</th><td><select multiple="multiple" name="categories">
164 177
 <option value="1">Entertainment</option>
165 178
 <option value="2">It&#39;s a test</option>
@@ -200,6 +213,12 @@ def __str__(self):
200 213
 <option value="2">Bob Woodward</option>
201 214
 </select></li>
202 215
 <li>Article: <textarea rows="10" cols="40" name="article">Hello.</textarea></li>
  216
+<li>Status: <select name="status">
  217
+<option value="" selected="selected">---------</option>
  218
+<option value="1">Draft</option>
  219
+<option value="2">Pending</option>
  220
+<option value="3">Live</option>
  221
+</select></li>
203 222
 <li>Categories: <select multiple="multiple" name="categories">
204 223
 <option value="1">Entertainment</option>
205 224
 <option value="2">It&#39;s a test</option>
@@ -232,6 +251,12 @@ def __str__(self):
232 251
 <option value="2">Bob Woodward</option>
233 252
 </select></li>
234 253
 <li>Article: <textarea rows="10" cols="40" name="article">Hello.</textarea></li>
  254
+<li>Status: <select name="status">
  255
+<option value="" selected="selected">---------</option>
  256
+<option value="1">Draft</option>
  257
+<option value="2">Pending</option>
  258
+<option value="3">Live</option>
  259
+</select></li>
235 260
 <li>Categories: <select multiple="multiple" name="categories">
236 261
 <option value="1" selected="selected">Entertainment</option>
237 262
 <option value="2">It&#39;s a test</option>
@@ -310,6 +335,12 @@ def __str__(self):
310 335
 <option value="2">Bob Woodward</option>
311 336
 </select></li>
312 337
 <li>Article: <textarea rows="10" cols="40" name="article"></textarea></li>
  338
+<li>Status: <select name="status">
  339
+<option value="" selected="selected">---------</option>
  340
+<option value="1">Draft</option>
  341
+<option value="2">Pending</option>
  342
+<option value="3">Live</option>
  343
+</select></li>
313 344
 <li>Categories: <select multiple="multiple" name="categories">
314 345
 <option value="1">Entertainment</option>
315 346
 <option value="2">It&#39;s a test</option>
@@ -329,6 +360,12 @@ def __str__(self):
329 360
 <option value="3">Carl Bernstein</option>
330 361
 </select></li>
331 362
 <li>Article: <textarea rows="10" cols="40" name="article"></textarea></li>
  363
+<li>Status: <select name="status">
  364
+<option value="" selected="selected">---------</option>
  365
+<option value="1">Draft</option>
  366
+<option value="2">Pending</option>
  367
+<option value="3">Live</option>
  368
+</select></li>
332 369
 <li>Categories: <select multiple="multiple" name="categories">
333 370
 <option value="1">Entertainment</option>
334 371
 <option value="2">It&#39;s a test</option>

0 notes on commit d9cc22b

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