Repository: jazzband/django-simple-history

Issue:
Cannot freeze CustomForeignKeyField when creating south migration
I'm using:
Python 2.6.9
Django==1.4.13
South==0.8.1
django-simple-history==1.5.2

When I try to do a ./manage.py schemamigration myapp --auto I get the following error:

 ! Cannot freeze field 'myapp.historicalmymodel.myothermodel_id'
 ! (this field has class simple_history.models.CustomForeignKeyField)

 ! South cannot introspect some fields; this is probably because they are custom
 ! fields. If they worked in 0.6 or below, this is because we have removed the
 ! models parser (it often broke things).
 ! To fix this, read http://south.aeracode.org/wiki/MyFieldsDontWork

I tried following the steps from the South page and added this:
from south.modelsinspector import add_introspection_rules
add_introspection_rules([], ["^simple_history.models.CustomForeignKeyField"])

That allowed me to create the migration, but then when I actually tried to run it I got the following error:
  File "/Users/mghantous/.virtual_envs/myproject/lib/python2.6/site-packages/django/db/models/fields/related.py", line 110, in resolve_related_class
    field.do_related_class(model, cls)
  File "/Users/mghantous/.virtual_envs/myproject/lib/python2.6/site-packages/simple_history/models.py", line 287, in do_related_class
    instance_type = cls.instance_type
AttributeError: type object 'HistoricalMyModel' has no attribute 'instance_type'

I should also mention MyModel inherits from another model that is Polymorphic. I think that might have something to do with it.

class MyBaseModel(PolymorphicModel)
class MyMode(MyBaseModel)

If I downgrade back to django-simple-history==1.3.0, everything works. I am not sure what is happening. Please look into this. Thanks.



Discussion:
I was being conservative and [checking for the `"__fake__"` module attribute](https://github.com/treyhunner/django-simple-history/blob/1.5.2/simple_history/models.py#L289) set by 1.7 migrations, assuming that was the only case the `instance_type` attribute would be missing. Apparently I was wrong.

@treyhunner, thoughts on removing that safety?

@mghantous, were you seeing errors with 1.4.0 as well? I think features added in that release introduced the `CustomForeignKey` concept. We happen to get it working with Django 1.7, but it is pretty dependent on internal API implementation.

Yes, I saw the issue in 1.4.0 as well.  I kept downgrading until it went away in 1.3.0.


Resolving patch:
diff --git a/simple_history/models.py b/simple_history/models.py
--- a/simple_history/models.py
+++ b/simple_history/models.py
@@ -23,6 +23,12 @@
     from datetime import datetime
     now = datetime.now
 from django.utils.translation import string_concat
+try:
+    from south.modelsinspector import add_introspection_rules
+except ImportError:
+    pass
+else:
+    add_introspection_rules([], ["^simple_history.models.CustomForeignKeyField"])
 from .manager import HistoryDescriptor
 
 registered_models = {}
@@ -286,8 +292,7 @@ def do_related_class(self, other, cls):
             try:
                 instance_type = cls.instance_type
             except AttributeError:  # when model is reconstituted for migration
-                if cls.__module__ != "__fake__":  # not from migrations, error
-                    raise
+                pass  # happens during migrations
             else:
                 self.related = RelatedObject(other, instance_type, self)
         transform_field(field)


Test patch: