The problem is that the _disable_audit_log_managers and _enable_audit_log_managers module functions in middleware.py only catch AttributeError when looking for an AuditLog instance. When the instance is a child model of a django-polymorphic, an ObjectDoesNotExist error is encountered first. The fix is safe and simple. Add ObjectDoesNotExist as an exception to also ignore.
[2018-03-14 11:09:33,471] ERROR django.request Internal Server Error: /exhibit_activity/new
Traceback (most recent call last):
File "/Users/massere/Projects/outreach-django/venv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/Users/massere/Projects/outreach-django/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "/Users/massere/Projects/outreach-django/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/massere/Projects/outreach-django/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/massere/Projects/outreach-django/venv/lib/python3.5/site-packages/nlm/occs/decorators.py", line 32, in wrapper
return view_func(request, *args, **kwargs)
File "/Users/massere/Projects/outreach-django/venv/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/massere/Projects/outreach-django/venv/lib/python3.5/site-packages/django/contrib/auth/mixins.py", line 56, in dispatch
return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs)
File "/Users/massere/Projects/outreach-django/venv/lib/python3.5/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/Users/massere/Projects/outreach-django/venv/lib/python3.5/site-packages/django/views/generic/edit.py", line 217, in post
return super(BaseCreateView, self).post(request, *args, **kwargs)
File "/Users/massere/Projects/outreach-django/venv/lib/python3.5/site-packages/django/views/generic/edit.py", line 183, in post
return self.form_valid(form)
File "/Users/massere/Projects/outreach-django/outreach/views.py", line 413, in form_valid
return super(ActivityFormMixin, self).form_valid(form)
File "/Users/massere/Projects/outreach-django/venv/lib/python3.5/site-packages/django/views/generic/edit.py", line 162, in form_valid
self.object = form.save()
File "/Users/massere/Projects/outreach-django/venv/lib/python3.5/site-packages/django/forms/models.py", line 468, in save
self.instance.save()
File "/Users/massere/Projects/outreach-django/venv/lib/python3.5/site-packages/polymorphic/models.py", line 83, in save
return super(PolymorphicModel, self).save(*args, **kwargs)
File "/Users/massere/Projects/outreach-django/venv/lib/python3.5/site-packages/django/db/models/base.py", line 807, in save
force_update=force_update, update_fields=update_fields)
File "/Users/massere/Projects/outreach-django/venv/lib/python3.5/site-packages/django/db/models/base.py", line 847, in save_base
update_fields=update_fields, raw=raw, using=using,
File "/Users/massere/Projects/outreach-django/venv/lib/python3.5/site-packages/django/dispatch/dispatcher.py", line 193, in send
for receiver in self._live_receivers(sender)
File "/Users/massere/Projects/outreach-django/venv/lib/python3.5/site-packages/django/dispatch/dispatcher.py", line 193, in <listcomp>
for receiver in self._live_receivers(sender)
File "/Users/massere/Projects/outreach-django/venv/lib/python3.5/site-packages/django/utils/functional.py", line 15, in _curried
return _curried_func(*(args + moreargs), **dict(kwargs, **morekwargs))
File "/Users/massere/Projects/outreach-django/venv/lib/python3.5/site-packages/audit_log/middleware.py", line 69, in _update_post_save_info
_enable_audit_log_managers(instance)
File "/Users/massere/Projects/outreach-django/venv/lib/python3.5/site-packages/audit_log/middleware.py", line 20, in _enable_audit_log_managers
if isinstance(getattr(instance, attr), AuditLogManager):
File "/Users/massere/Projects/outreach-django/venv/lib/python3.5/site-packages/polymorphic/models.py", line 175, in accessor_function
attr = model._base_objects.get(pk=self.pk)
File "/Users/massere/Projects/outreach-django/venv/lib/python3.5/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/massere/Projects/outreach-django/venv/lib/python3.5/site-packages/django/db/models/query.py", line 380, in get
self.model._meta.object_name
outreach.models.DoesNotExist: TrainingActivity matching query does not exist.
The problem is that the _disable_audit_log_managers and _enable_audit_log_managers module functions in middleware.py only catch AttributeError when looking for an AuditLog instance. When the instance is a child model of a django-polymorphic, an ObjectDoesNotExist error is encountered first. The fix is safe and simple. Add ObjectDoesNotExist as an exception to also ignore.