Last active
February 4, 2020 14:52
-
-
Save malikalbeik/ddbf71f315aaeef497156220cd159604 to your computer and use it in GitHub Desktop.
get an object's url in django admin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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