Skip to content

Commit 4648ebf

Browse files
authored
import ContentType via property / django_apps.get_model (#630)
* import ContentType via property / django_apps.get_model instead of direct import * CHANGES.rst
1 parent c341cf2 commit 4648ebf

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Unreleased
55
----------
66
- Add simple filtering if provided a minutes argument in `clean_duplicate_history` (gh-606)
77
- Add setting to convert `FileField` to `CharField` instead of `TextField` (gh-623)
8+
- import model `ContentType` in `SimpleHistoryAdmin` using `django_apps.get_model`
9+
to avoid possible `AppRegistryNotReady` exception (gh-630)
810

911
2.8.0 (2019-12-02)
1012
------------------

simple_history/admin.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import django
44
from django import http
5+
from django.apps import apps as django_apps
56
from django.conf import settings
67
from django.conf.urls import url
78
from django.contrib import admin
89
from django.contrib.admin import helpers
910
from django.contrib.admin.utils import unquote
10-
from django.contrib.contenttypes.models import ContentType
1111
from django.core.exceptions import PermissionDenied
1212
from django.shortcuts import get_object_or_404, render
1313
from django.urls import reverse
@@ -80,7 +80,9 @@ def history_view(self, request, object_id, extra_context=None):
8080
for list_entry in action_list:
8181
setattr(list_entry, history_list_entry, value_for_entry(list_entry))
8282

83-
content_type = ContentType.objects.get_by_natural_key(*USER_NATURAL_KEY)
83+
content_type = self.content_type_model_cls.objects.get_by_natural_key(
84+
*USER_NATURAL_KEY
85+
)
8486
admin_user_view = "admin:%s_%s_change" % (
8587
content_type.app_label,
8688
content_type.model,
@@ -196,7 +198,9 @@ def history_form_view(self, request, object_id, version_id, extra_context=None):
196198
"has_absolute_url": False,
197199
"form_url": "",
198200
"opts": model._meta,
199-
"content_type_id": ContentType.objects.get_for_model(self.model).id,
201+
"content_type_id": self.content_type_model_cls.objects.get_for_model(
202+
self.model
203+
).id,
200204
"save_as": self.save_as,
201205
"save_on_top": self.save_on_top,
202206
"root_path": getattr(self.admin_site, "root_path", None),
@@ -216,3 +220,9 @@ def save_model(self, request, obj, form, change):
216220
"""Set special model attribute to user for reference after save"""
217221
obj._history_user = request.user
218222
super(SimpleHistoryAdmin, self).save_model(request, obj, form, change)
223+
224+
@property
225+
def content_type_model_cls(self):
226+
"""Returns the ContentType model class.
227+
"""
228+
return django_apps.get_model("contenttypes.contenttype")

0 commit comments

Comments
 (0)