django-simple-history: Admin Integration broken: ContentType matching query does not exist.

Describe the bug Hi, Sorry if this is a user error on my side and I’m wasting your time, but I’m completely stuck at the moment to get the admin panel working. I could easily install and integrate django-simple-history into my app:

  • the middleware kicks in OK
  • records are being made
  • I can access the logs from Django Shell

… but I can’t get the Django Admin/History view to work.

To Reproduce Steps to reproduce the behavior:

  1. Go to http://127.0.0.1:8000/myapp/admin/ASD/asd/1/change/
  2. Click on 'History in the top-right corner ’
  3. See error

Expected behavior See the history of changes as shown in the documentation over here

Screenshots N/A

Environment:

  • OS: Windows 10
  • Browser (if applicable): Firefox 65.0.2
  • Django Simple History Version: 2.7.0
  • Django Version: 2.1.5
  • Database Version: SQLite (development) | PostgreSQL (production - hadn’t tested yet)

Additional context

django-simple-history works OK:

Python 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 13:35:33) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from ASD.models import ASD
>>> e = ASD.objects.get(pk=1)
>>> e.history.all()
<QuerySet [<HistoricalASD: John Appleseed (s12345) | 2019-03-20 as of 2019-03-02 17:15:22.610706+00:00>, <HistoricalASD: The Administrator (admin) | 2019-02-27 as of 2019-03-02 17:14:14.612195+00:00>]>

Traceback thrown on Django Admin/History view

(couple of applications have been removed for brevity and sensitive data have been obfuscated)

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/myapp/admin/ASD/asd/1/history/

Django Version: 2.1.5
Python Version: 3.6.7
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rest_framework',
 'simple_history',
 'ASD.apps.AsdConfig',
 'django_extensions']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'simple_history.middleware.HistoryRequestMiddleware']



Traceback:

File "C:\Users\theriverman\.virtualenvs\myapp-back-1wJT2xkN\lib\site-packages\django\contrib\contenttypes\models.py" in get_by_natural_key
  19.             ct = self._cache[self.db][(app_label, model)]

During handling of the above exception (('authsystem', 'user')), another exception occurred:

File "C:\Users\theriverman\.virtualenvs\myapp-back-1wJT2xkN\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Users\theriverman\.virtualenvs\myapp-back-1wJT2xkN\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\theriverman\.virtualenvs\myapp-back-1wJT2xkN\lib\site-packages\django\core\handlers\base.py" in _get_response
  124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\theriverman\.virtualenvs\myapp-back-1wJT2xkN\lib\site-packages\django\contrib\admin\options.py" in wrapper
  604.                 return self.admin_site.admin_view(view)(*args, **kwargs)

File "C:\Users\theriverman\.virtualenvs\myapp-back-1wJT2xkN\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "C:\Users\theriverman\.virtualenvs\myapp-back-1wJT2xkN\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)

File "C:\Users\theriverman\.virtualenvs\myapp-back-1wJT2xkN\lib\site-packages\django\contrib\admin\sites.py" in inner
  223.             return view(request, *args, **kwargs)

File "C:\Users\theriverman\.virtualenvs\myapp-back-1wJT2xkN\lib\site-packages\simple_history\admin.py" in history_view
  75.         content_type = ContentType.objects.get_by_natural_key(*USER_NATURAL_KEY)

File "C:\Users\theriverman\.virtualenvs\myapp-back-1wJT2xkN\lib\site-packages\django\contrib\contenttypes\models.py" in get_by_natural_key
  21.             ct = self.get(app_label=app_label, model=model)

File "C:\Users\theriverman\.virtualenvs\myapp-back-1wJT2xkN\lib\site-packages\django\db\models\manager.py" in manager_method
  82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "C:\Users\theriverman\.virtualenvs\myapp-back-1wJT2xkN\lib\site-packages\django\db\models\query.py" in get
  399.                 self.model._meta.object_name

Exception Type: DoesNotExist at /myapp/admin/ASD/asd/1/history/
Exception Value: ContentType matching query does not exist.

admin.py

from django.contrib import admin
from simple_history.admin import SimpleHistoryAdmin
from .models import ASD


class AsdHistoryAdmin(SimpleHistoryAdmin):
    list_display = ['id', 'subject', 'date', 'manager', 'is_closed']
    history_list_display = ['id']
    search_fields = ['id', 'subject']

admin.site.register(ASD, PddHistoryAdmin)

models.py

(Removed attribute arguments for brevity)

class ASD(models.Model):
    def get_tasks(self):
        return ASDSubTask.objects.filter(parent_asd=self)

    objects = models.Manager()

    # General Fields
    subject = models.ForeignKey(User, on_delete=models.CASCADE)
    manager = models.ForeignKey(User, on_delete=models.CASCADE)
    date = models.DateField()
    is_closed = models.BooleanField()
    description = models.TextField()
    quick_notes = models.CharField()
    followup_required = models.BooleanField()
    followup_date = models.DateField()
    tasks = property(get_tasks)

    # Misc.
    id = models.AutoField(primary_key=True, verbose_name="ID")
    date_created = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)
    history = HistoricalRecords(verbose_name="ASD History")

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15 (2 by maintainers)

Most upvoted comments

I’ve also encountered the same problem and the reason why is that I declare all of the Django apps my project with a mix of lowercase and uppercase letters and they get registered in ContentTypes with the name attribute on the apps config class, which is the same as the app’s name. For example, If I declare an AuthApp with a custom user class named User and a config class such as:

class AuthappConfig(AppConfig): 
    default_auto_field = "django.db.models.BigAutoField"
    name = "AuthApp"

the ContentType object created is app_label="AuthApp" and "model="user". So, the model name is lower case but not the app’s name and django-simple-history defines the USER_NATURAL_KEY` by making both of names lowercase resulting in them not being found.

I’m facing the same issue. Do we have any update on the fix?