Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #10792 -- Ensured that ModelChoiceFields don't provide an empty…

… option when the underlying field has blank=False and there is a default value available. Thanks to carljm for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10729 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 5663258de13975e28406233328a9e51c8bc1b768 1 parent f824ecc
Russell Keith-Magee authored May 10, 2009
5  django/forms/models.py
@@ -896,7 +896,10 @@ class ModelChoiceField(ChoiceField):
896 896
     def __init__(self, queryset, empty_label=u"---------", cache_choices=False,
897 897
                  required=True, widget=None, label=None, initial=None,
898 898
                  help_text=None, to_field_name=None, *args, **kwargs):
899  
-        self.empty_label = empty_label
  899
+        if required and (initial is not None):
  900
+            self.empty_label = None
  901
+        else:
  902
+            self.empty_label = empty_label
900 903
         self.cache_choices = cache_choices
901 904
 
902 905
         # Call Field instead of ChoiceField __init__() because we don't need
4  docs/ref/forms/fields.txt
@@ -786,6 +786,10 @@ example::
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
 
26  tests/regressiontests/forms/models.py
@@ -24,6 +24,17 @@ class ChoiceModel(models.Model):
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
 
@@ -105,4 +116,19 @@ class FileForm(django_forms.Form):
105 116
 >>> obj.def_date
106 117
 datetime.date(1999, 3, 2)
107 118
 >>> shutil.rmtree(temp_storage_location)
  119
+
  120
+In a ModelForm with a ModelChoiceField, if the model's ForeignKey has blank=False and a default,
  121
+no empty option is created (regression test for #10792).
  122
+
  123
+First we need at least one instance of ChoiceOptionModel:
  124
+
  125
+>>> ChoiceOptionModel.objects.create(name='default')
  126
+<ChoiceOptionModel: ChoiceOptionModel object>
  127
+
  128
+>>> class ChoiceFieldForm(ModelForm):
  129
+...     class Meta:
  130
+...         model = ChoiceFieldModel
  131
+>>> list(ChoiceFieldForm().fields['choice'].choices)
  132
+[(1, u'ChoiceOptionModel object')]
  133
+
108 134
 """}

0 notes on commit 5663258

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