17
Define a custom method on the admin class and reference that in your list_display
tuple.
from django.core.urlresolvers import reverse
class MyCommentsAdmin(admin.ModelAdmin):
list_display = ('id', 'name', 'comment', 'content_type', 'object_link', 'ip_address', 'submit_date', 'is_public', 'is_removed')
list_select_related = True
def object_link(self, obj):
ct = obj.content_type
url = reverse('admin:%s_%s_change' % (ct.app_label, ct.model), args=(obj.id,))
return '<a href="%s">%s</a>' % (url, obj.id)
object_link.allow_tags = True
Note I’ve added list_select_related=True
, as the object_link method references the content_type model, so it would otherwise cause a whole load of extra queries.
3
This was a good answer but it has changed significantly.
This answer is more applicable to Django 3.2.3
.
from django.urls import reverse
from django.utils.safestring import mark_safe
class MyCommentsAdmin(admin.ModelAdmin):
list_display = (
'id', 'name', 'comment', 'content_type', 'object_link', 'ip_address',
'submit_date', 'is_public', 'is_removed'
)
def object_link(self, obj):
app_label = obj._meta.app_label
model_label = obj._meta.model_name
url = reverse(
f'admin:{app_label}_{model_label}_change', args=(obj.id,)
)
return mark_safe(f'<a href="{url}">{obj.id}</a>')
- Passing arguments to management.call_command on a django view
- Django: Auto-generating a list of files in a directory
- How to check if a Django user is still logged in from the client side only?
- Django limit_choices_to on user group
Source:stackexchange.com