Skip to content

Instantly share code, notes, and snippets.

@malikalbeik
Last active February 4, 2020 14:52
Show Gist options
  • Save malikalbeik/ddbf71f315aaeef497156220cd159604 to your computer and use it in GitHub Desktop.
Save malikalbeik/ddbf71f315aaeef497156220cd159604 to your computer and use it in GitHub Desktop.
get an object's url in django admin.
from django.contrib.admin.models import DELETION
from django.utils.html import escape
from django.urls import reverse
from django.utils.safestring import mark_safe
def object_link(self, obj):
if obj.action_flag == DELETION:
link = escape(obj.object_repr)
else:
ct = obj.content_type
link = '<a href="%s">%s</a>' % (
reverse('admin:%s_%s_change' % (ct.app_label, ct.model), args=[obj.object_id]),
escape(obj.object_repr),
)
return mark_safe(link)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment