Repository: jazzband/django-simple-history

Issue:
Select from Model.history, then use .instance, then chase a foreign key is broken
**Describe the bug**
A clear and concise description of what the bug is.

In 3.1 HistoricForeignKey was introduced which is intended to work with Model.history.as_of(timepoint) and this works.

In 3.1.1, calling `.instance` on a historic record lookup does not populate `_as_of` which is necessary for HistoricForeignKey to work.  You can query history with as_of(timepoint) and things work properly, but querying Model.history directly then converting to an instance cannot chase the foreign keys properly.

My workaround in another project:

```
            if isinstance(value, Value.history.model):
                # work around https://github.com/jazzband/django-simple-history/issues/983
                # queries beginning with as_of properly inject, but direct selections
                # converted to instances do not... :|  I missed something!!
                setattr(value, "_as_of", value.modified_at)
                # then value.instance properly works
```


Discussion:
So I'm thinking the Historical model MAY have enough information in it to work without the extra attribute but I will have to play with it.

Resolving patch:
diff --git a/simple_history/models.py b/simple_history/models.py
--- a/simple_history/models.py
+++ b/simple_history/models.py
@@ -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)
 
 
@@ -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)


Test patch:
diff --git a/simple_history/tests/tests/test_models.py b/simple_history/tests/tests/test_models.py
--- a/simple_history/tests/tests/test_models.py
+++ b/simple_history/tests/tests/test_models.py
@@ -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")