Ticket #3257: related_choice_fields_moved_trunk_resync.patch
File related_choice_fields_moved_trunk_resync.patch, 7.6 KB (added by , 18 years ago) |
---|
-
django/db/models/fields/related.py
553 553 setattr(cls, related.get_accessor_name(), ForeignRelatedObjectsDescriptor(related)) 554 554 555 555 def formfield(self, **kwargs): 556 defaults = {' choices': self.get_choices_default(), 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}556 defaults = {'queryset': self.rel.to._default_manager.all(), 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} 557 557 defaults.update(kwargs) 558 return forms. ChoiceField(**defaults)558 return forms.ModelChoiceField(**defaults) 559 559 560 560 class OneToOneField(RelatedField, IntegerField): 561 561 def __init__(self, to, to_field=None, **kwargs): … … 619 619 cls._meta.one_to_one_field = self 620 620 621 621 def formfield(self, **kwargs): 622 defaults = {' choices': self.get_choices_default(), 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}622 defaults = {'queryset': self.rel.to._default_manager.all(), 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} 623 623 defaults.update(kwargs) 624 return forms. ChoiceField(**kwargs)624 return forms.ModelChoiceField(**kwargs) 625 625 626 626 class ManyToManyField(RelatedField, Field): 627 627 def __init__(self, to, **kwargs): … … 742 742 # MultipleChoiceField takes a list of IDs. 743 743 if kwargs.get('initial') is not None: 744 744 kwargs['initial'] = [i._get_pk_val() for i in kwargs['initial']] 745 defaults = {' choices': self.get_choices_default(), 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}745 defaults = {'queryset' : self.rel.to._default_manager.all(), 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} 746 746 defaults.update(kwargs) 747 return forms.M ultipleChoiceField(**defaults)747 return forms.ModelMultipleChoiceField(**defaults) 748 748 749 749 class ManyToOneRel(object): 750 750 def __init__(self, to, field_name, num_in_admin=3, min_num_in_admin=None, -
django/newforms/models.py
4 4 """ 5 5 6 6 from forms import BaseForm, DeclarativeFieldsMetaclass, SortedDictFromList 7 from fields import ChoiceField, MultipleChoiceField 8 from widgets import Select, SelectMultiple 7 9 8 __all__ = ('save_instance', 'form_for_model', 'form_for_instance', 'form_for_fields') 10 __all__ = ('save_instance', 'form_for_model', 'form_for_instance', 'form_for_fields', 11 'ModelChoiceField', 'ModelMultipleChoiceField') 9 12 10 13 def model_save(self, commit=True): 11 14 """ … … 33 36 for f in opts.fields: 34 37 if isinstance(f, models.AutoField): 35 38 continue 36 setattr(instance, f. attname, clean_data[f.name])39 setattr(instance, f.name, clean_data[f.name]) 37 40 if commit: 38 41 instance.save() 39 42 for f in opts.many_to_many: … … 96 99 "Returns a Form class for the given list of Django database field instances." 97 100 fields = SortedDictFromList([(f.name, f.formfield()) for f in field_list]) 98 101 return type('FormForFields', (BaseForm,), {'base_fields': fields}) 102 103 class ModelChoiceField(ChoiceField): 104 def __init__(self, queryset, **kwargs ): 105 self.model = queryset.model 106 ChoiceField.__init__(self, choices=[(obj._get_pk_val(), str(obj)) for obj in queryset], **kwargs) 107 108 def clean(self, value): 109 new_value = ChoiceField.clean(self, value) 110 if not new_value: 111 return None 112 try: 113 new_value = self.model._default_manager.get(pk=new_value) 114 except: 115 raise ValidationError(gettext(u'Select a valid choice. %s is not one of the available choices.') % value) 116 return new_value 117 118 class ModelMultipleChoiceField(MultipleChoiceField): 119 def __init__(self, queryset, **kwargs ): 120 self.model = queryset.model 121 MultipleChoiceField.__init__(self, choices=[(obj._get_pk_val(), str(obj)) for obj in queryset], **kwargs) 122 123 def clean(self, value): 124 new_value = MultipleChoiceField.clean(self, value) 125 if not new_value: 126 return [] 127 return self.model._default_manager.filter(pk__in=new_value) -
tests/modeltests/model_forms/models.py
24 24 25 25 from django.db import models 26 26 27 class Poll(models.Model): 28 question = models.CharField(maxlength=200) 29 def __str__(self): 30 return "Q: %s " % self.question 31 27 32 class Category(models.Model): 28 33 name = models.CharField(maxlength=20) 29 34 url = models.CharField('The URL', maxlength=40) … … 282 287 >>> Category.objects.get(id=3) 283 288 <Category: Third> 284 289 """} 290 291 # ModelChoiceField ################################################################# 292 293 >>> from django.newforms import * 294 295 >>> p = Poll(question="Test Question") 296 >>> p.save() 297 >>> p2 = Poll(question="Second Test Question") 298 >>> p2.save() 299 300 >>> f = ModelChoiceField(Poll.objects.all()) 301 >>> f.clean('') 302 Traceback (most recent call last): 303 ... 304 ValidationError: [u'This field is required.'] 305 >>> f.clean(None) 306 Traceback (most recent call last): 307 ... 308 ValidationError: [u'This field is required.'] 309 >>> f.clean(0) 310 Traceback (most recent call last): 311 ... 312 ValidationError: [u'Select a valid choice. 0 is not one of the available choices.'] 313 >>> f.clean(1) 314 <Poll: Q: Test Question > 315 >>> f.clean(2) 316 <Poll: Q: Second Test Question > 317 318 >>> f = ModelChoiceField(Poll.objects.filter( pk=1 ), required=False) 319 >>> print f.clean('') 320 None 321 >>> f.clean('') 322 >>> f.clean('1') 323 <Poll: Q: Test Question > 324 >>> f.clean('2') 325 Traceback (most recent call last): 326 ... 327 ValidationError: [u'Select a valid choice. 2 is not one of the available choices.'] 328 329 # ModelMultipleChoiceField ######################################################### 330 331 >>> f = ModelMultipleChoiceField(Poll.objects.all()) 332 >>> f.clean(None) 333 Traceback (most recent call last): 334 ... 335 ValidationError: [u'This field is required.'] 336 >>> f.clean([]) 337 Traceback (most recent call last): 338 ... 339 ValidationError: [u'This field is required.'] 340 >>> f.clean([1]) 341 [<Poll: Q: Test Question >] 342 >>> f.clean([2]) 343 [<Poll: Q: Second Test Question >] 344 >>> f.clean(['1']) 345 [<Poll: Q: Test Question >] 346 >>> f.clean(['1', '2']) 347 [<Poll: Q: Test Question >, <Poll: Q: Second Test Question >] 348 >>> f.clean([1, '2']) 349 [<Poll: Q: Test Question >, <Poll: Q: Second Test Question >] 350 >>> f.clean((1, '2')) 351 [<Poll: Q: Test Question >, <Poll: Q: Second Test Question >] 352 >>> f.clean('hello') 353 Traceback (most recent call last): 354 ... 355 ValidationError: [u'Enter a list of values.'] 356 >>> f = ModelMultipleChoiceField(Poll.objects.all(), required=False) 357 >>> f.clean([]) 358 [] 359 >>> f.clean(()) 360 [] 361 >>> f.clean(['3']) 362 Traceback (most recent call last): 363 ... 364 ValidationError: [u'Select a valid choice. 3 is not one of the available choices.'] 365 >>> f.clean(['3', '4']) 366 Traceback (most recent call last): 367 ... 368 ValidationError: [u'Select a valid choice. 3 is not one of the available choices.'] 369 >>> f.clean(['1', '5']) 370 Traceback (most recent call last): 371 ... 372 ValidationError: [u'Select a valid choice. 5 is not one of the available choices.'] 373 """}