Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove use of SubFieldBase. #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions uuidfield/fields.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import uuid

from django import forms
from django.db.models import Field, SubfieldBase
from django.db.models import Field
try:
from django.utils.encoding import smart_unicode
except ImportError:
Expand Down Expand Up @@ -32,6 +32,26 @@ def __len__(self):
return len(self.__str__())


class Creator(object):
"""
Field descriptor that calls the to_python method on assignment.

This matches the Django<=1.9 fields.subclassing.Creator class.
See the Django 1.8 release notes where SubFieldBase was deprecated for
more: https://docs.djangoproject.com/en/1.10/releases/1.8/#subfieldbase
"""
def __init__(self, field):
self.field = field

def __get__(self, obj, type=None):
if obj is None:
return self
return obj.__dict__[self.field.name]

def __set__(self, obj, value):
obj.__dict__[self.field.name] = self.field.to_python(value)


class UUIDField(Field):
"""
A field which stores a UUID value in hex format. This may also have the
Expand All @@ -40,7 +60,6 @@ class UUIDField(Field):
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,
Expand Down Expand Up @@ -69,6 +88,10 @@ def __init__(self, version=4, node=None, clock_seq=None,
self.namespace, self.name = namespace, name
super(UUIDField, self).__init__(*args, **kwargs)

def contribute_to_class(self, cls, name, **kwargs):
super(UUIDField, self).contribute_to_class(cls, name, **kwargs)
setattr(cls, self.name, Creator(self))

def deconstruct(self):
name, path, args, kwargs = super(UUIDField, self).deconstruct()
del kwargs['max_length']
Expand Down