from django.db import models
from django import forms
from sanetime import sanetime

class SaneTimeFormField(forms.DateTimeField):
    pass

class SaneTimeField(models.BigIntegerField):

    description = "A field to hold sanetimes (i.e. microseconds since epoch)."

    __metaclass__ = models.SubfieldBase

    def __init__(self, verbose_name=None, name=None, auto_now=False, auto_now_add=False, **kwargs):
        self.auto_now, self.auto_now_add = auto_now, auto_now_add
        if auto_now or auto_now_add:
            kwargs['editable'] = False
            kwargs['blank'] = True
        super(SaneTimeField, self).__init__(verbose_name, name, **kwargs)

    def pre_save(self, model_instance, add):
        if self.auto_now or (self.auto_now_add and add):
            value = sanetime()
            setattr(model_instance, self.attname, value)
            return value
        else:
            return super(SaneTimeField, self).pre_save(model_instance, add)

    def to_python(self, value):
        if value is not None:
            if not isinstance(value, sanetime):
                value = sanetime(value)
            return value
        return super(SaneTimeField,self).to_python(value)

    def get_prep_value(self, value):
        if value is not None:
            return int(value)
        return super(SaneTimeField,self).get_prep_value(value)


    #def formfield(self, **kwargs):
        #defaults = {'form_class': SaneTimeFormField}
        #defaults.update(kwargs)
        #return super(SaneTimeFormField, self).formfield(**defaults)



@@ -1548,9 +1548,154 @@
 aults)%0A%0A
+from south.modelsinspector import add_introspection_rules%0Aadd_introspection_rules(%5B%5D, %5B%22%5Edjextras%5C.fields%5C.sanetime_field%5C.SaneTimeField%22%5D)%0A    %0A
 %0A

make it south-able