diff --git a/README.rst b/README.rst index c828b1e..dfb667e 100644 --- a/README.rst +++ b/README.rst @@ -11,7 +11,7 @@ Provides a BitField like class (using a BigIntegerField) for your Django models. Requirements ============ -* Django >= 1.4 +* Django >= 1.10 * PostgreSQL (see notes) **Notes:** diff --git a/bitfield/__init__.py b/bitfield/__init__.py index 9088139..0d2ac6f 100644 --- a/bitfield/__init__.py +++ b/bitfield/__init__.py @@ -6,6 +6,7 @@ from bitfield.models import Bit, BitHandler, CompositeBitField, BitField # NOQA +default_app_config = 'bitfield.apps.BitFieldConfig' try: VERSION = __import__('pkg_resources') \ diff --git a/bitfield/apps.py b/bitfield/apps.py new file mode 100644 index 0000000..dabe0ce --- /dev/null +++ b/bitfield/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class BitFieldAppConfig(AppConfig): + name = 'bitfield' + verbose_name = "Bit Field" diff --git a/bitfield/models.py b/bitfield/models.py index d42a83d..564eba6 100644 --- a/bitfield/models.py +++ b/bitfield/models.py @@ -4,12 +4,6 @@ from django.db.models import signals from django.db.models.fields import Field, BigIntegerField -from django.db.models.fields.subclassing import Creator -try: - from django.db.models.fields.subclassing import SubfieldBase -except ImportError: - # django 1.2 - from django.db.models.fields.subclassing import LegacyConnection as SubfieldBase # NOQA from bitfield.forms import BitFormField from bitfield.query import BitQueryLookupWrapper @@ -60,13 +54,20 @@ def values(self): return list(self.itervalues()) -class BitFieldCreator(Creator): +class BitFieldCreator(object): """ + A placeholder class that provides a way to set the attribute on the model. Descriptor for BitFields. Checks to make sure that all flags of the instance match the class. This is to handle the case when caching an older version of the instance and a newer version of the class is available (usually during deploys). """ + def __init__(self, field): + self.field = field + + def __set__(self, obj, value): + obj.__dict__[self.field.name] = self.field.to_python(value) + def __get__(self, obj, type=None): if obj is None: return BitFieldFlags(self.field.flags) @@ -77,26 +78,13 @@ def __get__(self, obj, type=None): return retval -class BitFieldMeta(SubfieldBase): - """ - Modified SubFieldBase to use our contribute_to_class method (instead of - monkey-patching make_contrib). This uses our BitFieldCreator descriptor - in place of the default. - - NOTE: If we find ourselves needing custom descriptors for fields, we could - make this generic. - """ - def __new__(cls, name, bases, attrs): - def contribute_to_class(self, cls, name): - BigIntegerField.contribute_to_class(self, cls, name) - setattr(cls, self.name, BitFieldCreator(self)) +class BitField(BigIntegerField): - new_class = super(BitFieldMeta, cls).__new__(cls, name, bases, attrs) - new_class.contribute_to_class = contribute_to_class - return new_class + def contribute_to_class(self, cls, name, **kwargs): + super(BitField, self).contribute_to_class(cls, name, **kwargs) + setattr(cls, self.name, BitFieldCreator(self)) -class BitField(six.with_metaclass(BitFieldMeta, BigIntegerField)): def __init__(self, flags, default=None, *args, **kwargs): if isinstance(flags, dict): # Get only integer keys in correct range diff --git a/setup.py b/setup.py index 1d09c30..a76e26e 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ setup( name='django-bitfield', - version='1.8.0', + version='1.9.0', author='DISQUS', author_email='opensource@disqus.com', url='https://github.com/disqus/django-bitfield', @@ -12,7 +12,7 @@ packages=find_packages(), zip_safe=False, install_requires=[ - 'Django>=1.4', + 'Django>=1.10', 'six', ], extras_require={