mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
[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
This commit is contained in:
@@ -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')]
|
||||
|
||||
"""}
|
||||
|
||||
Reference in New Issue
Block a user