Skip to content

Commit a947655

Browse files
committed
[issue-983] querying history, convert to instance, chase foreign key on history_date
1 parent 46c5f2c commit a947655

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

simple_history/models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ def get_queryset(self, **hints) -> QuerySet:
673673
None,
674674
)
675675
if history and histmgr:
676-
return histmgr.as_of(history._as_of)
676+
return histmgr.as_of(getattr(history, "_as_of", history.history_date))
677677
return super().get_queryset(**hints)
678678

679679

@@ -708,7 +708,9 @@ def get_queryset(self):
708708
None,
709709
)
710710
if history and histmgr:
711-
queryset = histmgr.as_of(history._as_of)
711+
queryset = histmgr.as_of(
712+
getattr(history, "_as_of", history.history_date)
713+
)
712714
else:
713715
queryset = super().get_queryset()
714716
return self._apply_rel_filters(queryset)

simple_history/tests/tests/test_models.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,3 +2012,16 @@ def test_historic_to_historic(self):
20122012
to_historic(ot1), TestOrganizationWithHistory.history.model
20132013
)
20142014
self.assertIsNone(to_historic(org))
2015+
2016+
# test querying directly from the history table and converting
2017+
# to an instance, it should chase the foreign key properly
2018+
# in this case if _as_of is not present we use the history_date
2019+
# https://github.com/jazzband/django-simple-history/issues/983
2020+
pt1h = TestHistoricParticipanToHistoricOrganization.history.all()[0]
2021+
pt1i = pt1h.instance
2022+
self.assertEqual(pt1i.organization.name, "modified")
2023+
pt1h = TestHistoricParticipanToHistoricOrganization.history.all().order_by(
2024+
"history_date"
2025+
)[0]
2026+
pt1i = pt1h.instance
2027+
self.assertEqual(pt1i.organization.name, "original")

0 commit comments

Comments
 (0)