Ticket #9209: 9209.diff

File 9209.diff, 3.9 KB (added by Karen Tracey, 15 years ago)
  • django/forms/models.py

     
    708708        try:
    709709            key = self.to_field_name or 'pk'
    710710            value = self.queryset.get(**{key: value})
    711         except self.queryset.model.DoesNotExist:
     711        # Bare except since we could get ValueError, ValidationError, or ???
     712        # if the form-supplied data is of the wrong type for the pk field
     713        except:
    712714            raise ValidationError(self.error_messages['invalid_choice'])
    713715        return value
    714716
     
    740742        for val in value:
    741743            try:
    742744                obj = self.queryset.get(pk=val)
    743             except self.queryset.model.DoesNotExist:
     745            # Bare except since we could get ValueError, ValidationError, or ???
     746            # if the form-supplied data is of the wrong type for the pk field
     747            except:
    744748                raise ValidationError(self.error_messages['invalid_choice'] % val)
    745749            else:
    746750                final_values.append(obj)
  • tests/regressiontests/forms/models.py

     
    1717class ChoiceModel(models.Model):
    1818    """For ModelChoiceField and ModelMultipleChoiceField tests."""
    1919    name = models.CharField(max_length=10)
     20   
     21class ChoiceModelCharPK(models.Model):
     22    """For ModelChoiceField and ModelMultipleChoiceField tests."""
     23    name = models.CharField(max_length=10, primary_key=True)
    2024
    2125class FileModel(models.Model):
    2226    file = models.FileField(upload_to='/')
  • tests/regressiontests/forms/fields.py

     
    13861386Traceback (most recent call last):
    13871387...
    13881388ValidationError: [u'Enter a valid date.']
     1389
     1390# ModelChoiceFieldField & ModelMultipleChoiceField ###############################
     1391
     1392# Create choices for the model choice field tests below.
     1393>>> from regressiontests.forms.models import ChoiceModel
     1394>>> ChoiceModel.objects.create(name='x')
     1395<ChoiceModel: ChoiceModel object>
     1396
     1397>>> from regressiontests.forms.models import ChoiceModelCharPK
     1398>>> ChoiceModelCharPK.objects.create(name='x')
     1399<ChoiceModelCharPK: ChoiceModelCharPK object>
     1400
     1401# ModelChoiceField ############################################################
     1402
     1403>>> f = ModelChoiceField(queryset=ChoiceModel.objects.all())
     1404>>> pk = ChoiceModel.objects.all()[0].pk
     1405>>> f.clean(pk)
     1406<ChoiceModel: ChoiceModel object>
     1407>>> f.clean('NotAnInteger')
     1408Traceback (most recent call last):
     1409...
     1410ValidationError: [u'Select a valid choice. That choice is not one of the available choices.']
     1411
     1412>>> f = ModelChoiceField(queryset=ChoiceModelCharPK.objects.all())
     1413>>> pk = ChoiceModelCharPK.objects.all()[0].pk
     1414>>> f.clean(pk)
     1415<ChoiceModelCharPK: ChoiceModelCharPK object>
     1416>>> f.clean('Z')
     1417Traceback (most recent call last):
     1418...
     1419ValidationError: [u'Select a valid choice. That choice is not one of the available choices.']
     1420
     1421>>> f = ModelMultipleChoiceField(queryset=ChoiceModel.objects.all())
     1422>>> pk = ChoiceModel.objects.all()[0].pk
     1423>>> f.clean([pk])
     1424[<ChoiceModel: ChoiceModel object>]
     1425>>> f.clean(['NotAnInteger'])
     1426Traceback (most recent call last):
     1427...
     1428ValidationError: [u'Select a valid choice. NotAnInteger is not one of the available choices.']
     1429
     1430>>> f = ModelMultipleChoiceField(queryset=ChoiceModelCharPK.objects.all())
     1431>>> pk = ChoiceModelCharPK.objects.all()[0].pk
     1432>>> f.clean([pk])
     1433[<ChoiceModelCharPK: ChoiceModelCharPK object>]
     1434>>> f.clean(['Z'])
     1435Traceback (most recent call last):
     1436...
     1437ValidationError: [u'Select a valid choice. Z is not one of the available choices.']
    13891438"""
Back to Top