Skip to content

Commit

Permalink
[issue-983] querying history, convert to instance, chase foreign key …
Browse files Browse the repository at this point in the history
…on history_date
  • Loading branch information
jeking3 committed May 8, 2022
1 parent 46c5f2c commit a947655
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ def get_queryset(self, **hints) -> QuerySet:
None,
)
if history and histmgr:
return histmgr.as_of(history._as_of)
return histmgr.as_of(getattr(history, "_as_of", history.history_date))
return super().get_queryset(**hints)


Expand Down Expand Up @@ -708,7 +708,9 @@ def get_queryset(self):
None,
)
if history and histmgr:
queryset = histmgr.as_of(history._as_of)
queryset = histmgr.as_of(
getattr(history, "_as_of", history.history_date)
)
else:
queryset = super().get_queryset()
return self._apply_rel_filters(queryset)
Expand Down
13 changes: 13 additions & 0 deletions simple_history/tests/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2012,3 +2012,16 @@ def test_historic_to_historic(self):
to_historic(ot1), TestOrganizationWithHistory.history.model
)
self.assertIsNone(to_historic(org))

# test querying directly from the history table and converting
# to an instance, it should chase the foreign key properly
# in this case if _as_of is not present we use the history_date
# https://github.com/jazzband/django-simple-history/issues/983
pt1h = TestHistoricParticipanToHistoricOrganization.history.all()[0]
pt1i = pt1h.instance
self.assertEqual(pt1i.organization.name, "modified")
pt1h = TestHistoricParticipanToHistoricOrganization.history.all().order_by(
"history_date"
)[0]
pt1i = pt1h.instance
self.assertEqual(pt1i.organization.name, "original")

0 comments on commit a947655

Please sign in to comment.