Skip to content

Commit fd220ec

Browse files
committed
[issue-983] querying history, convert to instance, chase foreign key on history_date
1 parent 720fd8a commit fd220ec

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

simple_history/models.py

Lines changed: 2 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,7 @@ 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(getattr(history, "_as_of", history.history_date))
712712
else:
713713
queryset = super().get_queryset()
714714
return self._apply_rel_filters(queryset)

simple_history/tests/tests/test_models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,3 +2012,14 @@ 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("history_date")[0]
2024+
pt1i = pt1h.instance
2025+
self.assertEqual(pt1i.organization.name, "original")

0 commit comments

Comments
 (0)