Skip to content

Commit

Permalink
Merge 9793a5e into 844e5f8
Browse files Browse the repository at this point in the history
  • Loading branch information
macro1 committed Oct 10, 2014
2 parents 844e5f8 + 9793a5e commit 270154f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
9 changes: 3 additions & 6 deletions simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def copy_fields(self, model):
# Don't allow reverse relations.
# ForeignKey knows best what datatype to use for the column
# we'll used that as soon as it's finalized by copying rel.to
field.__class__ = get_custom_fk_class(type(field))
field.__class__ = CustomForeignKeyField
field.rel.related_name = '+'
field.null = True
field.blank = True
Expand Down Expand Up @@ -243,7 +243,8 @@ def get_history_user(self, instance):
return None


class ForeignKeyMixin(object):
class CustomForeignKeyField(models.ForeignKey):

def get_attname(self):
return self.name

Expand Down Expand Up @@ -312,10 +313,6 @@ def contribute_to_class(self, cls, name):
RelatedField.contribute_to_class(self, cls, name)


def get_custom_fk_class(parent_type):
return type(str('CustomForeignKey'), (ForeignKeyMixin, parent_type), {})


def transform_field(field):
"""Customize field appropriately for use in historical model"""
field.name = field.attname
Expand Down
5 changes: 5 additions & 0 deletions simple_history/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,8 @@ class UnicodeVerboseName(models.Model):

class Meta:
verbose_name = '\u570b'


class CustomFKError(models.Model):
fk = models.ForeignKey(SecondLevelInheritedModel)
history = HistoricalRecords()
6 changes: 5 additions & 1 deletion simple_history/tests/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
except ImportError: # django 1.4 compatibility
from django.contrib.auth.models import User
from django.db.models.loading import get_model
from django.db.migrations import state
from django.test import TestCase
from django.core.files.base import ContentFile

Expand All @@ -18,7 +19,7 @@
FileModel, Document, Book, HistoricalPoll, Library, State, AbstractBase,
ConcreteAttr, ConcreteUtil, SelfFK, Temperature, WaterLevel,
ExternalModel1, ExternalModel3, UnicodeVerboseName, HistoricalChoice,
HistoricalState
HistoricalState, HistoricalCustomFKError
)
from ..external.models import ExternalModel2, ExternalModel4

Expand Down Expand Up @@ -469,3 +470,6 @@ def test_import_related(self):
def test_string_related(self):
field_object = HistoricalState._meta.get_field_by_name('library_id')[0]
self.assertEqual(field_object.related.model, State)

def test_state_serialization_of_customfk(self):
state.ModelState.from_model(HistoricalCustomFKError)

0 comments on commit 270154f

Please sign in to comment.