foxfirefey: Fox stealing an egg. (Default)
[personal profile] foxfirefey posting in [community profile] django_dev
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.
From:
Anonymous( )Anonymous This account has disabled anonymous posting.
OpenID( )OpenID You can comment on this post while signed in with an account from many other sites, once you have confirmed your email address. Sign in using OpenID.
User
Account name:
Password:
If you don't have an account you can create one now.
Subject:
HTML doesn't work in the subject.

Message:

 
Notice: This account is set to log the IP addresses of everyone who comments.
Links will be displayed as unclickable URLs to help prevent spam.

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   

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags