Skip to content

Commit

Permalink
Removed docs on select_related_history_tracked_objs()
Browse files Browse the repository at this point in the history
See discussion:
jazzband#1128 (comment)

Also prefixed the method with `_`, to mark it as not being part of the
public API.
  • Loading branch information
ddabble committed Mar 11, 2024
1 parent 8513231 commit 84c195e
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 37 deletions.
2 changes: 0 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ Unreleased
lists are sorted by the related object. This should help prevent flaky tests. (gh-1128)
- ``diff_against()`` has a new keyword argument, ``foreign_keys_are_objs``;
see usage in the docs under "History Diffing" (gh-1128)
- Added ``HistoricalQuerySet.select_related_history_tracked_objs()``; see usage in the
docs under "Prefetching Related Objects of Historical Records" (gh-1128)

3.5.0 (2024-02-19)
------------------
Expand Down
4 changes: 2 additions & 2 deletions docs/history_diffing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ the model modifications.
the value of the deleted object's primary key.

Note that this will add extra database queries for each related field that's been
changed - as long as the related objects have not been prefetched (using e.g.
`select_related_history_tracked_objs() <Prefetching Related Objects>`_).
changed - as long as the related objects have not been prefetched
(using e.g. ``select_related()``).

A couple examples showing the difference:

Expand Down
30 changes: 0 additions & 30 deletions docs/querying_history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -225,33 +225,3 @@ You can also prefetch the objects with this relationship using something like th
Poll.objects.filter(something).prefetch_related(Prefetch('history', queryset=Poll.history.order_by('-history_date'),
to_attr='ordered_histories')
.. _`Prefetching Related Objects`:
Prefetching Related Objects of Historical Records
-------------------------------------------------
If you find yourself wanting to access the related objects of some historical records,
but without inducing additional database queries,
``HistoricalQuerySet`` has a method called ``select_related_history_tracked_objs()``
that you can use.
This is a convenience method that calls `select_related()`_ with all the names of
the model's history-tracked ``ForeignKey`` fields.
For example:
.. code-block:: python
class Membership(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE)
group = models.ForeignKey(Group, on_delete=models.CASCADE)
history = HistoricalRecords(excluded_fields=["person"])
# This will prefetch `group` (not `person`, since it's excluded)
Membership.history.select_related_history_tracked_objs()
Prefetching historical many-to-many relations is currently not supported.
.. _select_related(): https://docs.djangoproject.com/en/stable/ref/models/querysets/#select-related
2 changes: 1 addition & 1 deletion simple_history/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def get_history_queryset(
# Only select_related when history_user is a ForeignKey (not a property)
qs = qs.select_related("history_user")
# Prefetch related objects to reduce the number of DB queries when diffing
qs = qs.select_related_history_tracked_objs()
qs = qs._select_related_history_tracked_objs()
return qs

def get_history_list_display(self, request) -> Sequence[str]:
Expand Down
2 changes: 1 addition & 1 deletion simple_history/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def latest_of_each(self):
)
return latest_historics

def select_related_history_tracked_objs(self):
def _select_related_history_tracked_objs(self):
"""
A convenience method that calls ``select_related()`` with all the names of
the model's history-tracked ``ForeignKey`` fields.
Expand Down
2 changes: 1 addition & 1 deletion simple_history/tests/tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def access_related_objs(records):

# With prefetching:
with self.assertNumQueries(1):
historical_records = Choice.history.select_related_history_tracked_objs()
historical_records = Choice.history._select_related_history_tracked_objs()
self.assertEqual(len(historical_records), num_choices)
with self.assertNumQueries(0):
access_related_objs(historical_records)

0 comments on commit 84c195e

Please sign in to comment.