Ticket #3268: select_widget_for_fields_with_choices.diff
File select_widget_for_fields_with_choices.diff, 8.0 KB (added by , 18 years ago) |
---|
-
django/db/models/fields/__init__.py
344 344 def formfield(self, **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.update({'widget': forms.Select(choices=self.get_choices())}) 347 349 defaults.update(kwargs) 348 350 return forms.CharField(**defaults) 349 351 … … 406 408 407 409 def formfield(self, **kwargs): 408 410 defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} 411 if self.choices: 412 defaults.update({'widget': forms.Select(choices=self.get_choices())}) 409 413 defaults.update(kwargs) 410 414 return forms.BooleanField(**defaults) 411 415 … … 425 429 426 430 def formfield(self, **kwargs): 427 431 defaults = {'max_length': self.maxlength, 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} 432 if self.choices: 433 defaults.update({'widget': forms.Select(choices=self.get_choices())}) 428 434 defaults.update(kwargs) 429 435 return forms.CharField(**defaults) 430 436 … … 503 509 504 510 def formfield(self, **kwargs): 505 511 defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} 512 if self.choices: 513 defaults.update({'widget': forms.Select(choices=self.get_choices())}) 506 514 defaults.update(kwargs) 507 515 return forms.DateField(**defaults) 508 516 … … 568 576 569 577 def formfield(self, **kwargs): 570 578 defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} 579 if self.choices: 580 defaults.update({'widget': forms.Select(choices=self.get_choices())}) 571 581 defaults.update(kwargs) 572 582 return forms.DateTimeField(**defaults) 573 583 … … 587 597 588 598 def formfield(self, **kwargs): 589 599 defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} 600 if self.choices: 601 defaults.update({'widget': forms.Select(choices=self.get_choices())}) 590 602 defaults.update(kwargs) 591 603 return forms.EmailField(**defaults) 592 604 … … 724 736 725 737 def formfield(self, **kwargs): 726 738 defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} 739 if self.choices: 740 defaults.update({'widget': forms.Select(choices=self.get_choices())}) 727 741 defaults.update(kwargs) 728 742 return forms.IntegerField(**defaults) 729 743 … … 763 777 def formfield(self, **kwargs): 764 778 from django.contrib.localflavor.usa.forms import USPhoneNumberField 765 779 defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} 780 if self.choices: 781 defaults.update({'widget': forms.Select(choices=self.get_choices())}) 766 782 defaults.update(kwargs) 767 783 return USPhoneNumberField(**defaults) 768 784 … … 796 812 797 813 def formfield(self, **kwargs): 798 814 defaults = {'required': not self.blank, 'widget': forms.Textarea, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} 815 if self.choices: 816 defaults.update({'widget': forms.Select(choices=self.get_choices())}) 799 817 defaults.update(kwargs) 800 818 return forms.CharField(**defaults) 801 819 … … 841 859 842 860 def formfield(self, **kwargs): 843 861 defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} 862 if self.choices: 863 defaults.update({'widget': forms.Select(choices=self.get_choices())}) 844 864 defaults.update(kwargs) 845 865 return forms.TimeField(**defaults) 846 866 … … 860 880 861 881 def formfield(self, **kwargs): 862 882 defaults = {'required': not self.blank, 'verify_exists': self.verify_exists, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} 883 if self.choices: 884 defaults.update({'widget': forms.Select(choices=self.get_choices())}) 863 885 defaults.update(kwargs) 864 886 return forms.URLField(**defaults) 865 887 -
tests/modeltests/model_forms/models.py
24 24 25 25 from django.db import models 26 26 27 ARTICLE_STATUS_CHOICES = ( 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 50 writer = models.ForeignKey(Writer) 45 51 article = models.TextField() 46 52 categories = models.ManyToManyField(Category, blank=True) 53 status = models.IntegerField(blank=True, choices=ARTICLE_STATUS_CHOICES, null=True) 47 54 48 55 def save(self): 49 56 import datetime … … 147 154 >>> w = Writer(name='Bob Woodward') 148 155 >>> w.save() 149 156 150 ManyToManyFields are represented by a MultipleChoiceField, and ForeignKeys are151 represented by a ChoiceField.157 ManyToManyFields are represented by a MultipleChoiceField, ForeignKeys and any 158 fields with choices defined are represented by a ChoiceField. 152 159 >>> ArticleForm = form_for_model(Article) 153 160 >>> f = ArticleForm(auto_id=False) 154 161 >>> print f … … 160 167 <option value="2">Bob Woodward</option> 161 168 </select></td></tr> 162 169 <tr><th>Article:</th><td><textarea 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's a test</option> … … 200 213 <option value="2">Bob Woodward</option> 201 214 </select></li> 202 215 <li>Article: <textarea 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's a test</option> … … 232 251 <option value="2">Bob Woodward</option> 233 252 </select></li> 234 253 <li>Article: <textarea 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's a test</option> … … 310 335 <option value="2">Bob Woodward</option> 311 336 </select></li> 312 337 <li>Article: <textarea 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's a test</option> … … 329 360 <option value="3">Carl Bernstein</option> 330 361 </select></li> 331 362 <li>Article: <textarea 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's a test</option>