Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View Django 1.2 UUIDField with South support
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
from django.db import models
 
import uuid
 
class UUIDField(models.CharField):
"""
A field which stores a UUID value in hex format. This may also have
the Boolean attribute 'auto' which will set the value on initial save to a
new UUID value (calculated using the UUID1 method). Note that while all
UUIDs are expected to be unique we enforce this with a DB constraint.
"""
# Modified from http://www.davidcramer.net/code/420/improved-uuidfield-in-django.html
__metaclass__ = models.SubfieldBase
def __init__(self, auto=False, *args, **kwargs):
if kwargs.get('primary_key', False):
assert auto, "Must pass auto=True when using UUIDField as primary key."
self.auto = auto
# Set this as a fixed value, we store UUIDs in text.
kwargs['max_length'] = 32
if auto:
# Do not let the user edit UUIDs if they are auto-assigned.
kwargs['editable'] = False
kwargs['blank'] = True
kwargs['unique'] = True
super(UUIDField, self).__init__(*args, **kwargs)
def db_type(self):
return 'uuid'
def pre_save(self, model_instance, add):
"""Ensures that we auto-set values if required. See CharField.pre_save."""
value = getattr(model_instance, self.attname, None)
if not value and self.auto:
# Assign a new value for this attribute if required.
value = uuid.uuid4().hex
setattr(model_instance, self.attname, value)
return value
 
def to_python(self, value):
if not value:
return None
if len(value) != 32:
value = value.replace('-', '')
assert len(value) == 32
return value
try:
from south.modelsinspector import add_introspection_rules
except ImportError:
pass
else:
add_introspection_rules([
(
[UUIDField], # Class(es) these apply to
[], # Positional arguments (not used)
{ # Keyword argument
"auto": ["auto", {"default": "False"}],
},
),
], ["^common\.fields\.UUIDField"]) # XXX Change this to where yours is stored. Better solution?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.