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

File multiple-model-choice-field.2.diff, 1.2 KB (added by Alex Gaynor, 16 years ago)
  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index 70f0631..711cac4 100644
    a b class ModelMultipleChoiceField(ModelChoiceField):  
    781781            return []
    782782        if not isinstance(value, (list, tuple)):
    783783            raise ValidationError(self.error_messages['list'])
    784         final_values = []
    785         for val in value:
     784        for pk in value:
    786785            try:
    787                 obj = self.queryset.get(pk=val)
    788             except self.queryset.model.DoesNotExist:
    789                 raise ValidationError(self.error_messages['invalid_choice'] % val)
     786                self.queryset.filter(pk=pk)
    790787            except ValueError:
    791                 raise ValidationError(self.error_messages['invalid_pk_value'] % val)
    792             else:
    793                 final_values.append(obj)
    794         return final_values
     788                raise ValidationError(self.error_messages['invalid_pk_value'] % pk)
     789        qs = self.queryset.filter(pk__in=value)
     790        pks = set([force_unicode(o.pk) for o in qs])
     791        for val in value:
     792            if force_unicode(val) not in pks:
     793                raise ValidationError(self.error_messages['invalid_choice'] % val)
     794        return qs
Back to Top