summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Smeaton <josh.smeaton@gmail.com>2015-02-12 13:25:12 +1100
committerJosh Smeaton <josh.smeaton@gmail.com>2015-02-13 10:01:06 +1100
commit1784c326b1040bc1957a6f9664fdd2a8647bde50 (patch)
tree204a94a33df101c4a7808692bb373c7848164435
parent03f4e2d90997aec4c8f57c5c9b7925d70194327e (diff)
[1.8.x] Fixed #24319 -- Added validation for UUID model field
Backport of de0241eb985c6dec978beda119fee353ef3e9604 from master
-rw-r--r--django/db/models/fields/__init__.py4
-rw-r--r--tests/model_fields/test_uuid.py9
2 files changed, 11 insertions, 2 deletions
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index e487d7abf5..31b0f26141 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -2390,12 +2390,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):
diff --git a/tests/model_fields/test_uuid.py b/tests/model_fields/test_uuid.py
index 4680d5dbb4..f06f4d02a7 100644
--- a/tests/model_fields/test_uuid.py
+++ b/tests/model_fields/test_uuid.py
@@ -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):