diff options
| author | Russell Keith-Magee <russell@keith-magee.com> | 2009-05-10 07:44:27 +0000 |
|---|---|---|
| committer | Russell Keith-Magee <russell@keith-magee.com> | 2009-05-10 07:44:27 +0000 |
| commit | 5663258de13975e28406233328a9e51c8bc1b768 (patch) | |
| tree | 17ce8c67ca5723b24460cfa2c32a9120920f7d0b /tests/regressiontests/forms/models.py | |
| parent | f824ecc363b9b83b0c98fd1b3f9704021894a1fb (diff) | |
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
Diffstat (limited to 'tests/regressiontests/forms/models.py')
| -rw-r--r-- | tests/regressiontests/forms/models.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/tests/regressiontests/forms/models.py b/tests/regressiontests/forms/models.py index 472a39d35e..3b47b50b00 100644 --- a/tests/regressiontests/forms/models.py +++ b/tests/regressiontests/forms/models.py @@ -24,6 +24,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(storage=temp_storage, upload_to='tests') @@ -105,4 +116,19 @@ u'class default value' >>> obj.def_date datetime.date(1999, 3, 2) >>> shutil.rmtree(temp_storage_location) + +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')] + """} |
