Skip to content

Commit

Permalink
Use ContentType.objects.db_manager
Browse files Browse the repository at this point in the history
  • Loading branch information
dcwatson committed Mar 22, 2024
1 parent 1d9f2a2 commit 9fa6c77
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.4.6

* Properly specify database alias for ContentType queries


## 3.4.5

* Properly specify database alias for Django queries in the history backends
Expand Down
2 changes: 1 addition & 1 deletion history/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .backends import get_backend, session # noqa
from .utils import get_history_model # noqa

__version__ = "3.4.5"
__version__ = "3.4.6"
__version_info__ = tuple(
int(num) if num.isdigit() else num
for num in re.findall(r"([a-z\d]+)", __version__, re.I)
Expand Down
2 changes: 1 addition & 1 deletion history/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class HistoryAdminMixin:

def show_history(self, request, queryset, extra_context=None):
model_class = queryset.model
ct = ContentType.objects.get_for_model(model_class)
ct = ContentType.objects.db_manager(queryset._db).get_for_model(model_class)
object_history = (
get_history_model()
.objects.filter(
Expand Down
2 changes: 1 addition & 1 deletion history/backends/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def clear(self):
self.execute("TRUNCATE {table};".format(table=HistoryModel._meta.db_table))

def create_trigger(self, model, trigger_type):
ct = ContentType.objects.get_for_model(model)
ct = ContentType.objects.db_manager(self.alias).get_for_model(model)
tr_name = self.trigger_name(model, trigger_type)
self.execute(
"DROP TRIGGER IF EXISTS {tr_name} ON {table};".format(
Expand Down
2 changes: 1 addition & 1 deletion history/backends/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def _json_changes(self, fields, trigger_type):

def create_trigger(self, model, trigger_type):
HistoryModel = get_history_model()
ct = ContentType.objects.get_for_model(model)
ct = ContentType.objects.db_manager(self.alias).get_for_model(model)
tr_name = self.trigger_name(model, trigger_type)
session_cols = []
session_values = []
Expand Down
7 changes: 5 additions & 2 deletions history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ def get_user(self):

def __str__(self):
# Use the ContentType cache instead of FK lookups every time.
ct = ContentType.objects.get_for_id(self.content_type_id)
ct = ContentType.objects.db_manager(self._state.db).get_for_id(
self.content_type_id
)
return "{} {}({})".format(
self.get_change_type_display(),
ct.model_class()._meta.label,
Expand Down Expand Up @@ -87,7 +89,8 @@ class Meta(AbstractObjectHistory.Meta):

class HistoryDescriptor:
def __get__(self, instance, owner=None):
ct = ContentType.objects.get_for_model(instance or owner)
using = instance._state.db if instance else None
ct = ContentType.objects.db_manager(using).get_for_model(instance or owner)
qs = get_history_model().objects.filter(content_type=ct)
if instance:
qs = qs.filter(object_id=instance.pk)
Expand Down

0 comments on commit 9fa6c77

Please sign in to comment.