Skip to content

Commit 4fafa18

Browse files
committed
Fix bug with related
2 parents 8521837 + 2932d7a commit 4fafa18

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ tip (unreleased)
55
----------------
66
- Extended availability of the ``as_of`` method to models as well as instances.
77
- Allow ``history_user`` on historical objects to be set by middleware.
8+
- Fixed error that occurs when a foreign key is designated using just the name of the model.
89

910
1.4.0 (2014-06-29)
1011
------------------

simple_history/models.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
apps = None
99
from django.db import models
1010
from django.db.models.fields.related import RelatedField
11+
from django.db.models.related import RelatedObject
1112
from django.conf import settings
1213
from django.contrib import admin
1314
from django.utils import importlib
@@ -184,6 +185,7 @@ def get_instance(self):
184185
)),
185186
'history_object': HistoricalObjectDescriptor(model),
186187
'instance': property(get_instance),
188+
'instance_type': model,
187189
'revert_url': revert_url,
188190
'__str__': lambda self: '%s as of %s' % (self.history_object,
189191
self.history_date)
@@ -294,7 +296,8 @@ def get_field(self, other, cls):
294296

295297
def do_related_class(self, other, cls):
296298
field = self.get_field(other, cls)
297-
299+
if not hasattr(self, 'related'):
300+
self.related = RelatedObject(other, cls.instance_type, self)
298301
transform_field(field)
299302
field.rel = None
300303

simple_history/tests/tests/test_models.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
AdminProfile, Bookcase, MultiOneToOne, Poll, Choice, Restaurant, Person,
1818
FileModel, Document, Book, HistoricalPoll, Library, State, AbstractBase,
1919
ConcreteAttr, ConcreteUtil, SelfFK, Temperature, WaterLevel,
20-
ExternalModel1, ExternalModel3, UnicodeVerboseName
20+
ExternalModel1, ExternalModel3, UnicodeVerboseName, HistoricalChoice,
21+
HistoricalState
2122
)
2223
from ..external.models import ExternalModel2, ExternalModel4
2324

@@ -460,3 +461,11 @@ def test_invalid_bases(self):
460461
invalid_bases = (AbstractBase, "InvalidBases")
461462
for bases in invalid_bases:
462463
self.assertRaises(TypeError, HistoricalRecords, bases=bases)
464+
465+
def test_import_related(self):
466+
field_object = HistoricalChoice._meta.get_field_by_name('poll_id')[0]
467+
self.assertEqual(field_object.related.model, Choice)
468+
469+
def test_string_related(self):
470+
field_object = HistoricalState._meta.get_field_by_name('library_id')[0]
471+
self.assertEqual(field_object.related.model, State)

0 commit comments

Comments
 (0)