from django import VERSION
from django.db.models import fields
from django.utils.six import string_types, with_metaclass

import recurrence
from recurrence import forms

try:
    from south.modelsinspector import add_introspection_rules
    add_introspection_rules([], [
        "^recurrence\.fields\.RecurrenceField",
    ])
except ImportError:
    pass


if VERSION >= (1, 8):
    _RecurrenceField = fields.Field
else:
    # Django < 1.8, deprecated code remove it after Django 1.9 release
    # in December 2015
    from django.db.models.fields.subclassing import SubfieldBase
    _RecurrenceField = with_metaclass(SubfieldBase, fields.Field)


class RecurrenceField(_RecurrenceField):
    """
    Field that stores a `recurrence.base.Recurrence` object to the
    database.
    """

    def get_internal_type(self):
        return 'TextField'

    def to_python(self, value):
        if value is None:
            return value
        if isinstance(value, recurrence.Recurrence):
            return value
        value = super(RecurrenceField, self).to_python(value) or u''
        return recurrence.deserialize(value)

    def from_db_value(self, value, *args, **kwargs):
        return self.to_python(value)

    def get_db_prep_value(self, value, connection=None, prepared=False):
        if isinstance(value, string_types):
            value = recurrence.deserialize(value)
        return recurrence.serialize(value)

    def value_to_string(self, obj):
        return self.get_db_prep_value(self._get_val_from_obj(obj))

    def formfield(self, **kwargs):
        defaults = {
            'form_class': forms.RecurrenceField,
            'widget': forms.RecurrenceWidget,
        }
        defaults.update(kwargs)
        return super(RecurrenceField, self).formfield(**defaults)

from django.db.models import fields
from django.utils.six import string_types
from django.db.models.fields.subclassing import Creator

import recurrence
from recurrence import forms

try:
    from south.modelsinspector import add_introspection_rules
    add_introspection_rules([], [
        "^recurrence\.fields\.RecurrenceField",
    ])
except ImportError:
    pass


# Do not use SubfieldBase meta class because is removed in Django 1.10

class RecurrenceField(fields.Field):
    """Field that stores a `recurrence.base.Recurrence` to the database."""

    def get_internal_type(self):
        return 'TextField'

    def to_python(self, value):
        if value is None or isinstance(value, recurrence.Recurrence):
            return value
        value = super(RecurrenceField, self).to_python(value) or u''
        return recurrence.deserialize(value)

    def from_db_value(self, value, *args, **kwargs):
        return self.to_python(value)

    def get_prep_value(self, value):
        if not isinstance(value, string_types):
            value = recurrence.serialize(value)
        return value

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

    def value_to_string(self, obj):
        return self.get_prep_value(self._get_val_from_obj(obj))

    def formfield(self, **kwargs):
        defaults = {
            'form_class': forms.RecurrenceField,
            'widget': forms.RecurrenceWidget,
        }
        defaults.update(kwargs)
        return super(RecurrenceField, self).formfield(**defaults)

Make tests pass for Django 1.8 and 1.9.