diff --git a/CHANGELOG.md b/CHANGELOG.md index d554643..5072042 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/history/__init__.py b/history/__init__.py index 6a265ba..9cc64f9 100644 --- a/history/__init__.py +++ b/history/__init__.py @@ -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) diff --git a/history/admin.py b/history/admin.py index b2f471c..da3b36e 100644 --- a/history/admin.py +++ b/history/admin.py @@ -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( diff --git a/history/backends/postgres.py b/history/backends/postgres.py index a6235d8..9650ee4 100644 --- a/history/backends/postgres.py +++ b/history/backends/postgres.py @@ -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( diff --git a/history/backends/sqlite.py b/history/backends/sqlite.py index 87b2e8f..d52eab3 100644 --- a/history/backends/sqlite.py +++ b/history/backends/sqlite.py @@ -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 = [] diff --git a/history/models.py b/history/models.py index dd23b0b..7f5f866 100644 --- a/history/models.py +++ b/history/models.py @@ -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, @@ -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)