Don't crash if the linked object is not in the admin (fixes #99)

This commit is contained in:
Stavros Korokithakis 2017-02-16 05:06:12 +02:00
parent c127415a25
commit 03def6cdb3
No known key found for this signature in database
GPG key ID: 26EA345ECD4C2A63

View file

@ -2,6 +2,7 @@ import json
from django.conf import settings
from django.core import urlresolvers
from django.urls.exceptions import NoReverseMatch
MAX = 75
@ -26,8 +27,12 @@ class LogEntryAdminMixin(object):
def resource_url(self, obj):
app_label, model = obj.content_type.app_label, obj.content_type.model
viewname = 'admin:%s_%s_change' % (app_label, model)
link = urlresolvers.reverse(viewname, args=[obj.object_id or obj.object_pk])
return u'<a href="%s">%s</a>' % (link, obj.object_repr)
try:
link = urlresolvers.reverse(viewname, args=[obj.object_id])
except NoReverseMatch:
return obj.object_repr
else:
return u'<a href="%s">%s</a>' % (link, obj.object_repr)
resource_url.allow_tags = True
resource_url.short_description = 'Resource'