Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[issue-983] querying history, convert to instance, chase foreign key on history_date #984

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")