Skip to content

Commit

Permalink
Merge 71c073f into 844e5f8
Browse files Browse the repository at this point in the history
  • Loading branch information
macro1 committed Oct 10, 2014
2 parents 844e5f8 + 71c073f commit 46bdf39
Show file tree
Hide file tree
Showing 3 changed files with 16 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()
9 changes: 8 additions & 1 deletion simple_history/tests/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import unicode_literals

from datetime import datetime, timedelta
from unittest import skipUnless

import django
try:
from django.contrib.auth import get_user_model
User = get_user_model()
Expand All @@ -18,7 +20,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 +471,8 @@ 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)

@skipUnless(django.get_version() >= "1.7", "Requires 1.7 migrations")
def test_state_serialization_of_customfk(self):
from django.db.migrations import state
state.ModelState.from_model(HistoricalCustomFKError)

0 comments on commit 46bdf39

Please sign in to comment.