foxfirefey: A fox colored like flame over an ornately framed globe (Default)
[personal profile] foxfirefey
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.

Profile

django_dev: The Django logo. (Default)
Developing with Django

April 2014

S M T W T F S
  12345
6789101112
131415161718 19
20212223242526
27282930   

Syndicate

RSS Atom

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags