Ticket #4237: radio_select_with_tests.diff
File radio_select_with_tests.diff, 5.7 KB (added by , 17 years ago) |
---|
-
django/db/models/fields/__init__.py
349 349 "Returns a django.newforms.Field instance for this database Field." 350 350 defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} 351 351 if self.choices: 352 defaults['widget'] = forms.Select(choices=self.get_choices()) 352 if self.radio_admin: 353 defaults['widget'] = forms.RadioSelect(choices=self.get_choices_default()) 354 else: 355 defaults['widget'] = forms.Select(choices=self.get_choices_default()) 353 356 defaults.update(kwargs) 354 357 return form_class(**defaults) 355 358 -
docs/newforms.txt
1310 1310 1311 1311 * If the model field has ``choices`` set, then the form field's ``widget`` 1312 1312 will be set to ``Select``, with choices coming from the model field's 1313 ``choices``. 1313 ``choices``. If the model field specifies ``radio_admin=True`` then the 1314 form field's ``widget`` will be ``RadioSelect`` instead. 1314 1315 1315 1316 Finally, note that you can override the form field used for a given model 1316 1317 field. See "Overriding the default field types" below. -
tests/modeltests/model_forms/models.py
51 51 article = models.TextField() 52 52 categories = models.ManyToManyField(Category, blank=True) 53 53 status = models.IntegerField(choices=ARTICLE_STATUS, blank=True, null=True) 54 radio_status = models.IntegerField(choices=ARTICLE_STATUS, radio_admin=True, blank=True, null=True) 54 55 55 56 def save(self): 56 57 import datetime … … 155 156 >>> w.save() 156 157 157 158 ManyToManyFields are represented by a MultipleChoiceField, ForeignKeys and any 158 fields with the 'choices' attribute are represented by a ChoiceField. 159 fields with the 'choices' attribute are represented by a ChoiceField. Fields 160 with a 'choices' attribute and 'radio_admin' set to True will use a RadioSelect 161 widget. 159 162 >>> ArticleForm = form_for_model(Article) 160 163 >>> f = ArticleForm(auto_id=False) 161 164 >>> print f … … 173 176 <option value="2">Pending</option> 174 177 <option value="3">Live</option> 175 178 </select></td></tr> 179 <tr><th>Radio status:</th><td><ul> 180 <li><label><input checked="checked" type="radio" name="radio_status" value="" /> None</label></li> 181 <li><label><input type="radio" name="radio_status" value="1" /> Draft</label></li> 182 <li><label><input type="radio" name="radio_status" value="2" /> Pending</label></li> 183 <li><label><input type="radio" name="radio_status" value="3" /> Live</label></li> 184 </ul></td></tr> 176 185 <tr><th>Categories:</th><td><select multiple="multiple" name="categories"> 177 186 <option value="1">Entertainment</option> 178 187 <option value="2">It's a test</option> … … 231 240 <option value="2">Pending</option> 232 241 <option value="3">Live</option> 233 242 </select></li> 243 <li>Radio status: <ul> 244 <li><label><input checked="checked" type="radio" name="radio_status" value="" /> None</label></li> 245 <li><label><input type="radio" name="radio_status" value="1" /> Draft</label></li> 246 <li><label><input type="radio" name="radio_status" value="2" /> Pending</label></li> 247 <li><label><input type="radio" name="radio_status" value="3" /> Live</label></li> 248 </ul></li> 234 249 <li>Categories: <select multiple="multiple" name="categories"> 235 250 <option value="1">Entertainment</option> 236 251 <option value="2">It's a test</option> … … 285 300 <option value="2">Pending</option> 286 301 <option value="3">Live</option> 287 302 </select></li> 303 <li>Radio status: <ul> 304 <li><label><input checked="checked" type="radio" name="radio_status" value="" /> None</label></li> 305 <li><label><input type="radio" name="radio_status" value="1" /> Draft</label></li> 306 <li><label><input type="radio" name="radio_status" value="2" /> Pending</label></li> 307 <li><label><input type="radio" name="radio_status" value="3" /> Live</label></li> 308 </ul></li> 288 309 <li>Categories: <select multiple="multiple" name="categories"> 289 310 <option value="1" selected="selected">Entertainment</option> 290 311 <option value="2">It's a test</option> … … 369 390 <option value="2">Pending</option> 370 391 <option value="3">Live</option> 371 392 </select></li> 393 <li>Radio status: <ul> 394 <li><label><input checked="checked" type="radio" name="radio_status" value="" /> None</label></li> 395 <li><label><input type="radio" name="radio_status" value="1" /> Draft</label></li> 396 <li><label><input type="radio" name="radio_status" value="2" /> Pending</label></li> 397 <li><label><input type="radio" name="radio_status" value="3" /> Live</label></li> 398 </ul></li> 372 399 <li>Categories: <select multiple="multiple" name="categories"> 373 400 <option value="1">Entertainment</option> 374 401 <option value="2">It's a test</option> … … 394 421 <option value="2">Pending</option> 395 422 <option value="3">Live</option> 396 423 </select></li> 424 <li>Radio status: <ul> 425 <li><label><input checked="checked" type="radio" name="radio_status" value="" /> None</label></li> 426 <li><label><input type="radio" name="radio_status" value="1" /> Draft</label></li> 427 <li><label><input type="radio" name="radio_status" value="2" /> Pending</label></li> 428 <li><label><input type="radio" name="radio_status" value="3" /> Live</label></li> 429 </ul></li> 397 430 <li>Categories: <select multiple="multiple" name="categories"> 398 431 <option value="1">Entertainment</option> 399 432 <option value="2">It's a test</option>