From 7b69ac2a9cb7888a1f15644aa5cb5e56cc7d5c98 Mon Sep 17 00:00:00 2001 From: Andrew Cutler Date: Thu, 15 Jan 2015 14:47:42 +1100 Subject: [PATCH 1/2] Bug fix: to_python now returns the __unicode__ method of StringUUID. The previous implementation of returning an instance breaks when UUIDField is primary_key=True, and querysets use prefetch_related on models with such a field. Resolves https://github.com/dcramer/django-uuidfield/issues/47 --- uuidfield/fields.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/uuidfield/fields.py b/uuidfield/fields.py index b49d76a..549d0ae 100644 --- a/uuidfield/fields.py +++ b/uuidfield/fields.py @@ -139,8 +139,8 @@ def to_python(self, value): if not value: return None # attempt to parse a UUID including cases in which value is a UUID - # instance already to be able to get our StringUUID in. - return StringUUID(smart_unicode(value), hyphenate=self.hyphenate) + # and return the __unicode__ representation of the StringUUID instance + return StringUUID(smart_unicode(value), hyphenate=self.hyphenate).__unicode__() def formfield(self, **kwargs): defaults = { From 930e1cfae6097b365e08a66cfcbc3f38084a81e2 Mon Sep 17 00:00:00 2001 From: Andrew Cutler Date: Thu, 15 Jan 2015 14:54:26 +1100 Subject: [PATCH 2/2] merge in changes from pypi version 0.5.0 --- uuidfield/fields.py | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/uuidfield/fields.py b/uuidfield/fields.py index 549d0ae..08ed4c2 100644 --- a/uuidfield/fields.py +++ b/uuidfield/fields.py @@ -22,6 +22,9 @@ def __init__(self, *args, **kwargs): super(StringUUID, self).__init__(*args, **kwargs) + def __unicode__(self): + return unicode(str(self)) + def __str__(self): if self.hyphenate: return super(StringUUID, self).__str__() @@ -29,35 +32,27 @@ def __str__(self): return self.hex def __len__(self): - return len(self.__str__()) + return len(self.__unicode__()) class UUIDField(Field): """ - 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. Note that while all UUIDs are expected to be unique we enforce - this with a DB constraint. + 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. """ # TODO: support binary storage types __metaclass__ = SubfieldBase def __init__(self, version=4, node=None, clock_seq=None, - namespace=None, name=None, auto=False, hyphenate=False, - *args, **kwargs): - assert version in (1, 3, 4, 5), "UUID version {ver}is not supported."\ - .format(ver=version) + namespace=None, name=None, auto=False, hyphenate=False, *args, **kwargs): + assert version in (1, 3, 4, 5), "UUID version %s is not supported." % version self.auto = auto self.version = version self.hyphenate = hyphenate - - if hyphenate: - # We store UUIDs in string format, which is fixed at 36 characters. - kwargs['max_length'] = 36 - else: - # We store UUIDs in hex format, which is fixed at 32 characters. - kwargs['max_length'] = 32 - + # We store UUIDs in hex format, which is fixed at 32 characters. + kwargs['max_length'] = 32 if auto: # Do not let the user edit UUIDs if they are auto-assigned. kwargs['editable'] = False @@ -126,7 +121,7 @@ def value_to_string(self, obj): if val is None: data = '' else: - data = str(val) + data = unicode(val) return data def to_python(self, value):