summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2009-05-11 00:01:40 +0000
committerRussell Keith-Magee <russell@keith-magee.com>2009-05-11 00:01:40 +0000
commitbc07a498fe789ed5c4057d56bac6ba50d7296ed1 (patch)
tree92b731955fe41d38dfb90f85d33d8adacbc94e87
parent0d930ee0e647cdc46517971a8b84a304adb1178e (diff)
[1.0.X] 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.
Merge of r10729 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10733 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/forms/models.py5
-rw-r--r--docs/ref/forms/fields.txt4
-rw-r--r--tests/regressiontests/forms/models.py26
3 files changed, 34 insertions, 1 deletions
diff --git a/django/forms/models.py b/django/forms/models.py
index 8df69a5b59..32a55d69ba 100644
--- a/django/forms/models.py
+++ b/django/forms/models.py
@@ -869,7 +869,10 @@ class ModelChoiceField(ChoiceField):
def __init__(self, queryset, empty_label=u"---------", cache_choices=False,
required=True, widget=None, label=None, initial=None,
help_text=None, to_field_name=None, *args, **kwargs):
- self.empty_label = empty_label
+ if required and (initial is not None):
+ self.empty_label = None
+ else:
+ self.empty_label = empty_label
self.cache_choices = cache_choices
# Call Field instead of ChoiceField __init__() because we don't need
diff --git a/docs/ref/forms/fields.txt b/docs/ref/forms/fields.txt
index dbb87f6012..09aef7d29c 100644
--- a/docs/ref/forms/fields.txt
+++ b/docs/ref/forms/fields.txt
@@ -752,6 +752,10 @@ example::
# No empty label
field2 = forms.ModelChoiceField(queryset=..., empty_label=None)
+ Note that if a ``ModelChoiceField`` is required and has a default
+ initial value, no empty choice is created (regardless of the value
+ of ``empty_label``).
+
``ModelMultipleChoiceField``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/tests/regressiontests/forms/models.py b/tests/regressiontests/forms/models.py
index cc066fc03d..66bb3b0847 100644
--- a/tests/regressiontests/forms/models.py
+++ b/tests/regressiontests/forms/models.py
@@ -18,6 +18,17 @@ class ChoiceModel(models.Model):
"""For ModelChoiceField and ModelMultipleChoiceField tests."""
name = models.CharField(max_length=10)
+class ChoiceOptionModel(models.Model):
+ """Destination for ChoiceFieldModel's ForeignKey.
+ Can't reuse ChoiceModel because error_message tests require that it have no instances."""
+ name = models.CharField(max_length=10)
+
+class ChoiceFieldModel(models.Model):
+ """Model with ForeignKey to another model, for testing ModelForm
+ generation with ModelChoiceField."""
+ choice = models.ForeignKey(ChoiceOptionModel, blank=False,
+ default=lambda: ChoiceOptionModel.objects.all()[0])
+
class FileModel(models.Model):
file = models.FileField(upload_to='/')
@@ -73,4 +84,19 @@ u'instance value'
datetime.date(1969, 4, 4)
>>> instance_form.initial['value']
12
+
+In a ModelForm with a ModelChoiceField, if the model's ForeignKey has blank=False and a default,
+no empty option is created (regression test for #10792).
+
+First we need at least one instance of ChoiceOptionModel:
+
+>>> ChoiceOptionModel.objects.create(name='default')
+<ChoiceOptionModel: ChoiceOptionModel object>
+
+>>> class ChoiceFieldForm(ModelForm):
+... class Meta:
+... model = ChoiceFieldModel
+>>> list(ChoiceFieldForm().fields['choice'].choices)
+[(1, u'ChoiceOptionModel object')]
+
"""}