Ticket #10792: 10792_r10524.diff
File 10792_r10524.diff, 2.7 KB (added by , 16 years ago) |
---|
-
django/forms/models.py
711 711 required=True, widget=None, label=None, initial=None, 712 712 help_text=None, to_field_name=None, *args, **kwargs): 713 713 self.empty_label = empty_label 714 if required and (initial is not None): 715 self.empty_label = None 714 716 self.cache_choices = cache_choices 715 717 716 718 # Call Field instead of ChoiceField __init__() because we don't need -
tests/regressiontests/forms/models.py
24 24 """For ModelChoiceField and ModelMultipleChoiceField tests.""" 25 25 name = models.CharField(max_length=10) 26 26 27 class ChoiceOptionModel(models.Model): 28 """Destination for ChoiceFieldModel's ForeignKey. 29 Can't reuse ChoiceModel because error_message tests require that it have no instances.""" 30 name = models.CharField(max_length=10) 31 32 class ChoiceFieldModel(models.Model): 33 """Model with ForeignKey to another model, for testing ModelForm 34 generation with ModelChoiceField.""" 35 choice = models.ForeignKey(ChoiceOptionModel, blank=False, 36 default=lambda: ChoiceOptionModel.objects.all()[0]) 37 27 38 class FileModel(models.Model): 28 39 file = models.FileField(storage=temp_storage, upload_to='tests') 29 40 … … 86 97 >>> instance_form.initial['value'] 87 98 12 88 99 >>> shutil.rmtree(temp_storage_location) 100 101 In a ModelForm with a ModelChoiceField, if the model's ForeignKey has blank=False and a default, 102 no empty option is created (regression test for #10792). 103 104 First we need at least one instance of ChoiceOptionModel: 105 106 >>> ChoiceOptionModel.objects.create(name='default') 107 <ChoiceOptionModel: ChoiceOptionModel object> 108 109 >>> class ChoiceFieldForm(ModelForm): 110 ... class Meta: 111 ... model = ChoiceFieldModel 112 >>> list(ChoiceFieldForm().fields['choice'].choices) 113 [(1, u'ChoiceOptionModel object')] 114 89 115 """} -
docs/ref/forms/fields.txt
786 786 # No empty label 787 787 field2 = forms.ModelChoiceField(queryset=..., empty_label=None) 788 788 789 Note that if a ``ModelChoiceField`` is required and has a default 790 initial value, no empty choice is created (regardless of the value 791 of ``empty_label``). 792 789 793 ``ModelMultipleChoiceField`` 790 794 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 791 795