Skip to content

Commit 4463dee

Browse files
committed
Patch CustomForeignKeyField.do_related_class method for migrations
1 parent 07ac1f1 commit 4463dee

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

simple_history/models.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,15 @@ def get_field(self, other, cls):
301301
def do_related_class(self, other, cls):
302302
field = self.get_field(other, cls)
303303
if not hasattr(self, 'related'):
304-
self.related = RelatedObject(other, cls.instance_type, self)
304+
try:
305+
instance_type = cls.instance_type
306+
except AttributeError: # when model is reconstituted for migration
307+
natural_key = "{app}.{model}".format(
308+
app=cls._meta.app_label,
309+
model=cls.__name__[10:],
310+
)
311+
instance_type = cls._meta.apps.get_model(natural_key)
312+
self.related = RelatedObject(other, instance_type, self)
305313
transform_field(field)
306314
field.rel = None
307315

simple_history/tests/tests/test_commands.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
from contextlib import contextmanager
22
from six.moves import cStringIO as StringIO
33
from datetime import datetime
4+
try:
5+
from unittest import skipUnless
6+
except ImportError:
7+
from unittest2 import skipUnless
8+
import django
49
from django.test import TestCase
510
from django.core import management
611
from simple_history import models as sh_models
@@ -101,3 +106,10 @@ def test_no_historical(self):
101106
stdout=out)
102107
self.assertIn(populate_history.Command.NO_REGISTERED_MODELS,
103108
out.getvalue())
109+
110+
111+
class TestMigrate(TestCase):
112+
113+
@skipUnless(django.get_version() >= "1.7", "Requires 1.7 migrations")
114+
def test_migrate_command(self):
115+
management.call_command('migrate', fake=True, stdout=StringIO())

0 commit comments

Comments
 (0)