Is it possible to add action button to ModelAdmin?

794 views
Skip to first unread message

rani....@snip.today

unread,
Sep 19, 2016, 4:13:55 AM9/19/16
to Wagtail support
Hi,

I built a simple ModelAdmin page to my posts and I would like to add more buttons to it.
In the explorer page it was pretty easy using the hooks: http://docs.wagtail.io/en/v1.5/reference/hooks.html#register-page-listing-buttons
I couldn't find how to do it in a ModelAdmin page.

Would appreciate you help!

Rani

Matthew Westcott

unread,
Sep 20, 2016, 6:10:58 AM9/20/16
to wag...@googlegroups.com
Hi Rani,
There's some new documentation about customising ModelAdmin, currently in review here:
https://github.com/torchbox/wagtail/pull/2902
In particular, I believe the section on button_helper_class will be relevant:
https://github.com/rkhleics/wagtail/blob/932392ba165d5077c626710b1a3635d67459943b/docs/reference/contrib/modeladmin/primer.rst#L345

Cheers,
- Matt

rani....@snip.today

unread,
Sep 20, 2016, 8:47:14 AM9/20/16
to Wagtail support
Hi,

Great! It works.

Is it possible to call JS function using my new button? :)

Thanks

On Tuesday, 20 September 2016 13:10:58 UTC+3, Matthew Westcott wrote:
Hi Rani,
There's some new documentation about customising ModelAdmin, currently in review here:
https://github.com/torchbox/wagtail/pull/2902
In particular, I believe the section on button_helper_class will be relevant:
https://github.com/rkhleics/wagtail/blob/932392ba165d5077c626710b1a3635d67459943b/docs/reference/contrib/modeladmin/primer.rst#L345

Cheers,
- Matt


rani....@snip.today

unread,
Sep 20, 2016, 8:49:54 AM9/20/16
to Wagtail support
My code, in case someone would need it in the future:

class MyButtonHelper(PageButtonHelper):
def test_button(self, pk, classnames_add=[], classnames_exclude=[]):
classnames = self.copy_button_classnames + classnames_add
cn = self.finalise_classname(classnames, classnames_exclude)
return {
'url': self.url_helper.get_action_url('copy', quote(pk)),
'label': _('test'),
'classname': cn,
'title': _('test this %s') % self.verbose_name,
}

def get_buttons_for_obj(self, obj, exclude=[], classnames_add=[],
classnames_exclude=[]):

pk = quote(getattr(obj, self.opts.pk.attname))
btns = super(MyButtonHelper, self).get_buttons_for_obj(obj, exclude, classnames_add, classnames_exclude)

if 'test' not in exclude:
btns.append(
self.test_button(pk, classnames_add, classnames_exclude)
)
return btns

Just add this to the modelAdmin:
button_helper_class = MyButtonHelper
Reply all
Reply to author
Forward
0 new messages