change list display link in django admin change list display link in django admin django django

change list display link in django admin


The solution was to override the init and set the list_display_links to None e.g.

class FooModelAdmin(admin.ModelAdmin):    fields = ('foo','bar')    list_display = ('foo_link','bar')    def foo_link(self,obj):        return u'<a href="/foos/%s/">%s</a>' % (obj.foo,obj)    foo_link.allow_tags = True    foo_link.short_description = "foo"    def __init__(self,*args,**kwargs):        super(FooModelAdmin, self).__init__(*args, **kwargs)        self.list_display_links = (None, )


I believe the correct way of doing it, is subclassing ChangeList and override the url_for_result method to create the correct change url you want.

Override the get_changelist in the admin.ModelAdmin subclass to return the new class:

from django.contrib.admin.views.main import ChangeListfrom django.contrib.admin.util import quoteclass FooChangeList(ChangeList):    def url_for_result(self, result):        pk = getattr(result, self.pk_attname)        return '/foos/foo/%d/' % (quote(pk))class FooAdmin(admin.ModelAdmin):    def get_changelist(self, request, **kwargs):        return FooChangeList


By default the first column of list display will be link to the admin edit page. If you want another column or columns to be that link, a very easy way is as follows:

class FooModelAdmin(admin.ModelAdmin):    list_display = ('foo_link', 'bar', 'another_bar', )    list_display_links = ('foo_link', 'another_bar', )

If foo_link is not a property of the model, it should be a callable like the following:

class FooModelAdmin(admin.ModelAdmin):    list_display = ('foo_link', 'bar', 'another_bar', )    list_display_links = ('foo_link', 'another_bar', )    def foo_link(self, obj):        return "%s blah blah" % obj.some_property # or anything you prefer e.g. an edit button

A full example from my project:

class SchoolTeacherAdmin(admin.ModelAdmin):    list_display = ('name', 'designation', 'school_name', 'school_code', 'date_of_birth', 'mobile', 'nid', 'edit', )    list_display_links = ('edit', )    def school_code(self, obj):        return obj.school.code    def school_name(self, obj):        return obj.school.name.upper()    def edit(self, obj):        return "Edit"