summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2011-02-19 14:47:33 +0000
committerRussell Keith-Magee <russell@keith-magee.com>2011-02-19 14:47:33 +0000
commit69a3bb5d473c864124e57541e9dde8496da3ac15 (patch)
tree70316655d8a63b98fbb025e04b2dcb7ad001395c /tests
parent626ad2c9ab9e02db8e3f19bd51654d749e84af02 (diff)
[1.2.X] Fixed #9161 -- Ensure that ModelMultipleChoiceField respects to_field_name in validation. Thanks to Honza for the report, and Gregor Müllegger for the patch.
Backport of r15587 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15588 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
-rw-r--r--tests/modeltests/model_forms/models.py19
1 files changed, 19 insertions, 0 deletions
diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py
index 4e7265ba74..d3a08ca760 100644
--- a/tests/modeltests/model_forms/models.py
+++ b/tests/modeltests/model_forms/models.py
@@ -1562,6 +1562,25 @@ ValidationError: [u'Select a valid choice. z is not one of the available choices
<tr><th><label for="id_description">Description:</label></th><td><input type="text" name="description" id="id_description" /></td></tr>
<tr><th><label for="id_url">The URL:</label></th><td><input id="id_url" type="text" name="url" maxlength="40" /></td></tr>
+# to_field_name should also work on ModelMultipleChoiceField ##################
+
+>>> field = ModelMultipleChoiceField(Inventory.objects.all(), to_field_name='barcode')
+>>> for choice in field.choices:
+... print choice
+(86, u'Apple')
+(22, u'Pear')
+(87, u'Core')
+>>> field.clean([86])
+[<Inventory: Apple>]
+
+>>> class SelectInventoryForm(forms.Form):
+... items = ModelMultipleChoiceField(Inventory.objects.all(), to_field_name='barcode')
+>>> form = SelectInventoryForm({'items': [87, 22]})
+>>> form.is_valid()
+True
+>>> form.cleaned_data
+{'items': [<Inventory: Pear>, <Inventory: Core>]}
+
# Model field that returns None to exclude itself with explicit fields ########
>>> class CustomFieldForExclusionForm(ModelForm):