ingari
ingari

Reputation: 11

Is there a way to add live link to Page-based listing view in Wagtail modeladmin?

I am using ModelAdmin to create a listing of items in an Article model which is based on Page. Is there an easy way to add a "live" link in the modeladmin listing similar to the live link that is show through the Page tree interface? Below is my class in wagtail_hooks.py

class ArticlePageModelAdmin(ModelAdmin):
    model = ArticlePage
    menu_label = 'Articles'  
    menu_icon = 'folder-open-inverse'  
    menu_order = 200  
    add_to_settings_menu = False  
    exclude_from_explorer = False  
    list_display = ('title',  'author','article_type', 'featured_status', 'first_published_at','live',)
    list_filter = ('article_type', 'featured_status', 'author')
    search_fields = ('title',)

modeladmin_register(ArticlePageModelAdmin)

Upvotes: 1

Views: 214

Answers (1)

allcaps
allcaps

Reputation: 11248

The list_display accepts a string representing an attribute on the ModelAdmin. This callable accepts a parameter obj that is the model instance. For example:

list_display = ['title', 'live_url']

def live_url(self, obj):
    return mark_safe(
        '<div class="status">'
             '<a href="{}" target="_blank" class="status-tag primary">live</a>'
         '</div>'.format(obj.get_url())
    )

I didn't look into pages that are draft (not published yet, or retracted). I also didn't do anything with latest revisions. There could be a newer revision. To get that right, reuse the methods that live on the page instance. Eg: status_string.

You get the idea, happy coding! ;)

Upvotes: 0

Related Questions