Skip to content

Commit

Permalink
Patch CustomForeignKeyField.do_related_class method for migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
macro1 committed Oct 11, 2014
1 parent 07ac1f1 commit 4463dee
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
10 changes: 9 additions & 1 deletion simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions simple_history/tests/tests/test_commands.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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())

0 comments on commit 4463dee

Please sign in to comment.