summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSanyam Khurana <sanyam.khurana01@gmail.com>2018-10-25 03:13:41 +0530
committerTim Graham <timograham@gmail.com>2018-10-24 20:26:57 -0400
commit83c7096f2a84450957615ef52ffed0bee5f72606 (patch)
tree9df0361459a07f40094cc7b9139f57369fcca9c3
parentfd49701ab9bb9352b549ec02600af1ee35794d40 (diff)
Fixed #29869 -- Made UUIDField.to_python() convert integers.
-rw-r--r--django/db/models/fields/__init__.py3
-rw-r--r--tests/model_fields/test_uuid.py16
2 files changed, 18 insertions, 1 deletions
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index 162d6ec716..04c7f002bc 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -2319,8 +2319,9 @@ class UUIDField(Field):
def to_python(self, value):
if value is not None and not isinstance(value, uuid.UUID):
+ input_form = 'int' if isinstance(value, int) else 'hex'
try:
- return uuid.UUID(value)
+ return uuid.UUID(**{input_form: value})
except (AttributeError, ValueError):
raise exceptions.ValidationError(
self.error_messages['invalid'],
diff --git a/tests/model_fields/test_uuid.py b/tests/model_fields/test_uuid.py
index 6b6af3ea7e..40fc82812f 100644
--- a/tests/model_fields/test_uuid.py
+++ b/tests/model_fields/test_uuid.py
@@ -64,6 +64,22 @@ class TestMethods(SimpleTestCase):
def test_to_python(self):
self.assertIsNone(models.UUIDField().to_python(None))
+ def test_to_python_int_values(self):
+ self.assertEqual(
+ models.UUIDField().to_python(0),
+ uuid.UUID('00000000-0000-0000-0000-000000000000')
+ )
+ # Works for integers less than 128 bits.
+ self.assertEqual(
+ models.UUIDField().to_python((2 ** 128) - 1),
+ uuid.UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')
+ )
+
+ def test_to_python_int_too_large(self):
+ # Fails for integers larger than 128 bits.
+ with self.assertRaises(exceptions.ValidationError):
+ models.UUIDField().to_python(2 ** 128)
+
class TestQuerying(TestCase):
def setUp(self):