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

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

    diff --git a/django/forms/models.py b/django/forms/models.py
    index 01bd912..450f95e 100644
    a b class ModelMultipleChoiceField(ModelChoiceField):  
    738738        if not isinstance(value, (list, tuple)):
    739739            raise ValidationError(self.error_messages['list'])
    740740        final_values = []
     741        qs = self.queryset.filter(pk__in=value)
     742        pks = set([force_unicode(o.pk) for o in qs])
    741743        for val in value:
    742             try:
    743                 obj = self.queryset.get(pk=val)
    744             except self.queryset.model.DoesNotExist:
     744            if force_unicode(val) not in pks:
    745745                raise ValidationError(self.error_messages['invalid_choice'] % val)
    746             else:
    747                 final_values.append(obj)
    748         return final_values
     746        return qs
Back to Top