Skip to content

Commit 18f00bd

Browse files
committed
Made admin history queryset overridable
1 parent 16b7de7 commit 18f00bd

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Unreleased
1111
``SimpleHistoryAdmin.object_history_list_template`` for overriding it (gh-1128)
1212
- Deprecated the undocumented template tag ``simple_history_admin_list.display_list()``;
1313
it will be removed in version 3.8 (gh-1128)
14+
- Added ``SimpleHistoryAdmin.get_history_queryset()`` for overriding which ``QuerySet``
15+
is used to list the historical records (gh-1128)
1416

1517
3.5.0 (2024-02-19)
1618
------------------

simple_history/admin.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any
2+
13
from django import http
24
from django.apps import apps as django_apps
35
from django.conf import settings
@@ -6,13 +8,15 @@
68
from django.contrib.admin.utils import unquote
79
from django.contrib.auth import get_permission_codename, get_user_model
810
from django.core.exceptions import PermissionDenied
11+
from django.db.models import QuerySet
912
from django.shortcuts import get_object_or_404, render
1013
from django.urls import re_path, reverse
1114
from django.utils.encoding import force_str
1215
from django.utils.html import mark_safe
1316
from django.utils.text import capfirst
1417
from django.utils.translation import gettext as _
1518

19+
from .manager import HistoryManager
1620
from .utils import get_history_manager_for_model, get_history_model_for_model
1721

1822
SIMPLE_HISTORY_EDIT = getattr(settings, "SIMPLE_HISTORY_EDIT", False)
@@ -47,10 +51,9 @@ def history_view(self, request, object_id, extra_context=None):
4751
pk_name = opts.pk.attname
4852
history = getattr(model, model._meta.simple_history_manager_attribute)
4953
object_id = unquote(object_id)
50-
historical_records = history.filter(**{pk_name: object_id})
51-
if not isinstance(history.model.history_user, property):
52-
# Only select_related when history_user is a ForeignKey (not a property)
53-
historical_records = historical_records.select_related("history_user")
54+
historical_records = self.get_history_queryset(
55+
request, history, pk_name, object_id
56+
)
5457
history_list_display = getattr(self, "history_list_display", [])
5558
# If no history was found, see whether this object even exists.
5659
try:
@@ -99,6 +102,25 @@ def history_view(self, request, object_id, extra_context=None):
99102
request, self.object_history_template, context, **extra_kwargs
100103
)
101104

105+
def get_history_queryset(
106+
self, request, history_manager: HistoryManager, pk_name: str, object_id: Any
107+
) -> QuerySet:
108+
"""
109+
Return a ``QuerySet`` of all historical records that should be listed in the
110+
``object_history_list_template`` template.
111+
This is used by ``history_view()``.
112+
113+
:param request:
114+
:param history_manager:
115+
:param pk_name: The name of the original model's primary key field.
116+
:param object_id: The primary key of the object whose history is listed.
117+
"""
118+
qs = history_manager.filter(**{pk_name: object_id})
119+
if not isinstance(history_manager.model.history_user, property):
120+
# Only select_related when history_user is a ForeignKey (not a property)
121+
qs = qs.select_related("history_user")
122+
return qs
123+
102124
def history_view_title(self, request, obj):
103125
if self.revert_disabled(request, obj) and not SIMPLE_HISTORY_EDIT:
104126
return _("View history: %s") % force_str(obj)

0 commit comments

Comments
 (0)