Skip to content

Commit

Permalink
Fix bug with related
Browse files Browse the repository at this point in the history
  • Loading branch information
macro1 committed Aug 8, 2014
2 parents 8521837 + 2932d7a commit 4fafa18
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ tip (unreleased)
----------------
- Extended availability of the ``as_of`` method to models as well as instances.
- Allow ``history_user`` on historical objects to be set by middleware.
- Fixed error that occurs when a foreign key is designated using just the name of the model.

1.4.0 (2014-06-29)
------------------
Expand Down
5 changes: 4 additions & 1 deletion simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
apps = None
from django.db import models
from django.db.models.fields.related import RelatedField
from django.db.models.related import RelatedObject
from django.conf import settings
from django.contrib import admin
from django.utils import importlib
Expand Down Expand Up @@ -184,6 +185,7 @@ def get_instance(self):
)),
'history_object': HistoricalObjectDescriptor(model),
'instance': property(get_instance),
'instance_type': model,
'revert_url': revert_url,
'__str__': lambda self: '%s as of %s' % (self.history_object,
self.history_date)
Expand Down Expand Up @@ -294,7 +296,8 @@ 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)
transform_field(field)
field.rel = None

Expand Down
11 changes: 10 additions & 1 deletion simple_history/tests/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
AdminProfile, Bookcase, MultiOneToOne, Poll, Choice, Restaurant, Person,
FileModel, Document, Book, HistoricalPoll, Library, State, AbstractBase,
ConcreteAttr, ConcreteUtil, SelfFK, Temperature, WaterLevel,
ExternalModel1, ExternalModel3, UnicodeVerboseName
ExternalModel1, ExternalModel3, UnicodeVerboseName, HistoricalChoice,
HistoricalState
)
from ..external.models import ExternalModel2, ExternalModel4

Expand Down Expand Up @@ -460,3 +461,11 @@ def test_invalid_bases(self):
invalid_bases = (AbstractBase, "InvalidBases")
for bases in invalid_bases:
self.assertRaises(TypeError, HistoricalRecords, bases=bases)

def test_import_related(self):
field_object = HistoricalChoice._meta.get_field_by_name('poll_id')[0]
self.assertEqual(field_object.related.model, Choice)

def test_string_related(self):
field_object = HistoricalState._meta.get_field_by_name('library_id')[0]
self.assertEqual(field_object.related.model, State)

0 comments on commit 4fafa18

Please sign in to comment.