From 1abf126e612fca1efec2986400e2996be8130d22 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sat, 19 Feb 2011 14:45:54 +0000 Subject: [PATCH] =?UTF-8?q?Fixed=20#9161=20--=20Ensure=20that=20ModelMulti?= =?UTF-8?q?pleChoiceField=20respects=20to=5Ffield=5Fname=20in=20validation?= =?UTF-8?q?.=20Thanks=20to=20Honza=20for=20the=20report,=20and=20Gregor=20?= =?UTF-8?q?M=C3=BCllegger=20for=20the=20patch.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.djangoproject.com/svn/django/trunk@15587 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/forms/models.py | 7 ++++--- tests/modeltests/model_forms/models.py | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/django/forms/models.py b/django/forms/models.py index 2a3f8bdff2..912bdf36a8 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -1006,13 +1006,14 @@ class ModelMultipleChoiceField(ModelChoiceField): return [] if not isinstance(value, (list, tuple)): raise ValidationError(self.error_messages['list']) + key = self.to_field_name or 'pk' for pk in value: try: - self.queryset.filter(pk=pk) + self.queryset.filter(**{key: pk}) except ValueError: raise ValidationError(self.error_messages['invalid_pk_value'] % pk) - qs = self.queryset.filter(pk__in=value) - pks = set([force_unicode(o.pk) for o in qs]) + qs = self.queryset.filter(**{'%s__in' % key: value}) + pks = set([force_unicode(getattr(o, key)) for o in qs]) for val in value: if force_unicode(val) not in pks: raise ValidationError(self.error_messages['invalid_choice'] % val) diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index b9b8d16c07..2e820a6779 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 +# 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]) +[] + +>>> 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': [, ]} + # Model field that returns None to exclude itself with explicit fields ######## >>> class CustomFieldForExclusionForm(ModelForm):