diff options
| author | Marc Tamlyn <marc.tamlyn@gmail.com> | 2014-07-15 10:35:29 +0100 |
|---|---|---|
| committer | Marc Tamlyn <marc.tamlyn@gmail.com> | 2014-09-16 10:08:09 +0100 |
| commit | ed7821231b7dbf34a6c8ca65be3b9bcbda4a0703 (patch) | |
| tree | 98657032a61094eb7ac9c46d5db5bc12d25d1fa0 /django/forms/fields.py | |
| parent | 0d1561d19739e83cf20150585c9e26894b428bad (diff) | |
Fixed #19463 -- Added UUIDField
Uses native support in postgres, and char(32) on other backends.
Diffstat (limited to 'django/forms/fields.py')
| -rw-r--r-- | django/forms/fields.py | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py index 2b44c73996..6403509fbb 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -9,6 +9,7 @@ import datetime import os import re import sys +import uuid import warnings from decimal import Decimal, DecimalException from io import BytesIO @@ -41,7 +42,7 @@ __all__ = ( 'BooleanField', 'NullBooleanField', 'ChoiceField', 'MultipleChoiceField', 'ComboField', 'MultiValueField', 'FloatField', 'DecimalField', 'SplitDateTimeField', 'IPAddressField', 'GenericIPAddressField', 'FilePathField', - 'SlugField', 'TypedChoiceField', 'TypedMultipleChoiceField' + 'SlugField', 'TypedChoiceField', 'TypedMultipleChoiceField', 'UUIDField', ) @@ -1224,3 +1225,25 @@ class SlugField(CharField): def clean(self, value): value = self.to_python(value).strip() return super(SlugField, self).clean(value) + + +class UUIDField(CharField): + default_error_messages = { + 'invalid': _('Enter a valid UUID.'), + } + + def prepare_value(self, value): + if isinstance(value, uuid.UUID): + return value.hex + return value + + def to_python(self, value): + value = super(UUIDField, self).to_python(value) + if value in self.empty_values: + return None + if not isinstance(value, uuid.UUID): + try: + value = uuid.UUID(value) + except ValueError: + raise ValidationError(self.error_messages['invalid'], code='invalid') + return value |
