Flask Admin extend "with select"-dropdown menu with custom button
Asked Answered
S

1

9

Im using the built-in view of flask admin. As you can see in the picture below: enter image description here

What Im trying is simple: I just want to extend the dropdown menu with a custom button. This button should perfome some action on all selected items. Is there are built-in function of flask where i can simple add an action button?

Saundrasaunter answered 19/4, 2018 at 12:41 Comment(1)
Im also open for other ways. Im still struggling. I have tried to modifiy the base.py file and the corresponding template but It seems really complicated and I coudln´t find a similar problem on the internetSaundrasaunter
N
12

Use the @action decorator. Simple example below, the text "Recalculate Charges" is what appears in the drop-down menu.

class TransactionView(AdminView):
    
    from flask_admin.actions import action

    @action('recalculate', 'Recalculate Charges', 'Are you sure you want to recalculate selected transactions(s)?')
    def action_recalculate(self, ids):
        count = 0
        for _id in ids:
            # Do some work with the id, e.g. call a service method
            transaction_service.recalculate_transaction(_id)
            count += 1
        flash("{0} transaction (s) charges recalculated".format(count))
Neutralization answered 20/4, 2018 at 9:44 Comment(3)
Thanks a lot. This is what I was looking for. Now Im able to iterate through the selected items and do what I want. You saved my day.Saundrasaunter
For someone new to or learning flask-admin this answer isn't easily digestible. References to models and importing necessary functions isn't clear. Just throwing that out there. Appreciate the answer though.Wallasey
Well I'm new to flask but I thought this example was everything I needed. Although added the import would have been nice: from flask_admin.actions import actionTrachyte

© 2022 - 2024 — McMap. All rights reserved.