Forked from reverie/Django 1.2 UUIDField with South support
Created
July 17, 2011 11:01
-
-
Save mattgrayson/1087458 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
def south_field_triple(self): | |
"Returns a suitable description of this field for South." | |
# We'll just introspect the _actual_ field. | |
from south.modelsinspector import introspector | |
field_class = "django.db.models.fields.CharField" | |
args, kwargs = introspector(self) | |
# That's our definition! | |
return (field_class, args, kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment