djangodjango-adminmodeladmin

change list display link in django admin


I am trying to change the link for an object in the django admin list display. Here is what I have so far:

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)
    domain_link.allow_tags = True
    domain_link.short_description = "foo"

This produces another link within the original list display link e.g.

<a href="/admin/app/model/pk/"><a href="/foos/foo/">Foo</a></a>

Solution

  • 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, )