Code

Ticket #10156: multiple-model-choice-field.diff

File multiple-model-choice-field.diff, 852 bytes (added by Alex, 5 years ago)
Line 
1diff --git a/django/forms/models.py b/django/forms/models.py
2index 01bd912..450f95e 100644
3--- a/django/forms/models.py
4+++ b/django/forms/models.py
5@@ -738,11 +738,9 @@ class ModelMultipleChoiceField(ModelChoiceField):
6         if not isinstance(value, (list, tuple)):
7             raise ValidationError(self.error_messages['list'])
8         final_values = []
9+        qs = self.queryset.filter(pk__in=value)
10+        pks = set([force_unicode(o.pk) for o in qs])
11         for val in value:
12-            try:
13-                obj = self.queryset.get(pk=val)
14-            except self.queryset.model.DoesNotExist:
15+            if force_unicode(val) not in pks:
16                 raise ValidationError(self.error_messages['invalid_choice'] % val)
17-            else:
18-                final_values.append(obj)
19-        return final_values
20+        return qs