diff --git a/simple_history/models.py b/simple_history/models.py index 1511cd90c..0987dbe5b 100644 --- a/simple_history/models.py +++ b/simple_history/models.py @@ -301,7 +301,15 @@ def get_field(self, other, cls): def do_related_class(self, other, cls): field = self.get_field(other, cls) if not hasattr(self, 'related'): - self.related = RelatedObject(other, cls.instance_type, self) + try: + instance_type = cls.instance_type + except AttributeError: # when model is reconstituted for migration + natural_key = "{app}.{model}".format( + app=cls._meta.app_label, + model=cls.__name__[10:], + ) + instance_type = cls._meta.apps.get_model(natural_key) + self.related = RelatedObject(other, instance_type, self) transform_field(field) field.rel = None diff --git a/simple_history/tests/tests/test_commands.py b/simple_history/tests/tests/test_commands.py index c211c175e..6a7aa79ac 100644 --- a/simple_history/tests/tests/test_commands.py +++ b/simple_history/tests/tests/test_commands.py @@ -1,6 +1,11 @@ from contextlib import contextmanager from six.moves import cStringIO as StringIO from datetime import datetime +try: + from unittest import skipUnless +except ImportError: + from unittest2 import skipUnless +import django from django.test import TestCase from django.core import management from simple_history import models as sh_models @@ -101,3 +106,10 @@ def test_no_historical(self): stdout=out) self.assertIn(populate_history.Command.NO_REGISTERED_MODELS, out.getvalue()) + + +class TestMigrate(TestCase): + + @skipUnless(django.get_version() >= "1.7", "Requires 1.7 migrations") + def test_migrate_command(self): + management.call_command('migrate', fake=True, stdout=StringIO())