foxfirefey: Fox stealing an egg. (Default)
foxfirefey ([personal profile] foxfirefey) wrote in [community profile] django_dev2010-04-28 02:38 pm
Entry tags:

Making a "non-built-in" link in the admin list view

This was something I found out how to do recently and got inspired to share! It's about how to add a link that's not a built in one (like the link to edit the model) to the list view in the admin. This can be useful in some cases.

First, in your model definition, add a function that returns the link that you want.

def external_link(self):
        """Returns a link for display in the admin table."""
        if self.article_link:
            return u'Link' % self.article_link
        else:
            return None
    external_link.allow_tags = True 
    external_link.short_description = "Link"


Adding the allow_tags attribute to this function lets the admin know it can display the raw HTML. The short_description attribute is optional and lets you specify what you want the column's title to be; if you don't have it, it'll be the function's name with underscores replaced by spaces.

Then, you can add it to the list display:

class NewsItemAdmin(admin.ModelAdmin):
    list_display =  ('title', 'date_published', 'external_link')


Tada! You now have a link in your admin's list display.