Fixed #24319 -- Added validation for UUID model field
This commit is contained in:
parent
d64baaef3b
commit
de0241eb98
|
@ -2382,12 +2382,12 @@ class UUIDField(Field):
|
|||
return "UUIDField"
|
||||
|
||||
def get_db_prep_value(self, value, connection, prepared=False):
|
||||
if isinstance(value, six.string_types):
|
||||
value = uuid.UUID(value.replace('-', ''))
|
||||
if isinstance(value, uuid.UUID):
|
||||
if connection.features.has_native_uuid_field:
|
||||
return value
|
||||
return value.hex
|
||||
if isinstance(value, six.string_types):
|
||||
return value.replace('-', '')
|
||||
return value
|
||||
|
||||
def to_python(self, value):
|
||||
|
|
|
@ -34,6 +34,15 @@ class TestSaveLoad(TestCase):
|
|||
loaded = NullableUUIDModel.objects.get()
|
||||
self.assertEqual(loaded.field, None)
|
||||
|
||||
def test_wrong_value(self):
|
||||
self.assertRaisesMessage(
|
||||
ValueError, 'badly formed hexadecimal UUID string',
|
||||
UUIDModel.objects.get, field='not-a-uuid')
|
||||
|
||||
self.assertRaisesMessage(
|
||||
ValueError, 'badly formed hexadecimal UUID string',
|
||||
UUIDModel.objects.create, field='not-a-uuid')
|
||||
|
||||
|
||||
class TestMigrations(TestCase):
|
||||
|
||||
|
|
Loading…
Reference in New Issue