Fixed #27148 -- Fixed ModelMultipleChoiceField crash with invalid UUID.

This commit is contained in:
Tim Graham 2016-10-31 15:21:05 -04:00 committed by GitHub
parent de91c172cf
commit 2f9861d823
3 changed files with 13 additions and 10 deletions

View File

@ -2371,20 +2371,17 @@ class UUIDField(Field):
if value is None: if value is None:
return None return None
if not isinstance(value, uuid.UUID): if not isinstance(value, uuid.UUID):
try: value = self.to_python(value)
value = uuid.UUID(value)
except AttributeError:
raise TypeError(self.error_messages['invalid'] % {'value': value})
if connection.features.has_native_uuid_field: if connection.features.has_native_uuid_field:
return value return value
return value.hex return value.hex
def to_python(self, value): def to_python(self, value):
if value and not isinstance(value, uuid.UUID): if not isinstance(value, uuid.UUID):
try: try:
return uuid.UUID(value) return uuid.UUID(value)
except ValueError: except (AttributeError, ValueError):
raise exceptions.ValidationError( raise exceptions.ValidationError(
self.error_messages['invalid'], self.error_messages['invalid'],
code='invalid', code='invalid',

View File

@ -40,17 +40,17 @@ class TestSaveLoad(TestCase):
self.assertIsNone(loaded.field) self.assertIsNone(loaded.field)
def test_pk_validated(self): def test_pk_validated(self):
with self.assertRaisesMessage(TypeError, 'is not a valid UUID'): with self.assertRaisesMessage(exceptions.ValidationError, 'is not a valid UUID'):
PrimaryKeyUUIDModel.objects.get(pk={}) PrimaryKeyUUIDModel.objects.get(pk={})
with self.assertRaisesMessage(TypeError, 'is not a valid UUID'): with self.assertRaisesMessage(exceptions.ValidationError, 'is not a valid UUID'):
PrimaryKeyUUIDModel.objects.get(pk=[]) PrimaryKeyUUIDModel.objects.get(pk=[])
def test_wrong_value(self): def test_wrong_value(self):
with self.assertRaisesMessage(ValueError, 'badly formed hexadecimal UUID string'): with self.assertRaisesMessage(exceptions.ValidationError, 'is not a valid UUID'):
UUIDModel.objects.get(field='not-a-uuid') UUIDModel.objects.get(field='not-a-uuid')
with self.assertRaisesMessage(ValueError, 'badly formed hexadecimal UUID string'): with self.assertRaisesMessage(exceptions.ValidationError, 'is not a valid UUID'):
UUIDModel.objects.create(field='not-a-uuid') UUIDModel.objects.create(field='not-a-uuid')

View File

@ -1,6 +1,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django import forms from django import forms
from django.core.exceptions import ValidationError
from django.test import TestCase from django.test import TestCase
from .models import UUIDPK from .models import UUIDPK
@ -27,3 +28,8 @@ class ModelFormBaseTest(TestCase):
msg = "The UUIDPK could not be changed because the data didn't validate." msg = "The UUIDPK could not be changed because the data didn't validate."
with self.assertRaisesMessage(ValueError, msg): with self.assertRaisesMessage(ValueError, msg):
form.save() form.save()
def test_model_multiple_choice_field_uuid_pk(self):
f = forms.ModelMultipleChoiceField(UUIDPK.objects.all())
with self.assertRaisesMessage(ValidationError, "'invalid_uuid' is not a valid UUID."):
f.clean(['invalid_uuid'])