starlette-admin icon indicating copy to clipboard operation
starlette-admin copied to clipboard

Question: How do we modify available actions for each item?

Open hasansezertasan opened this issue 8 months ago • 6 comments

How do we modify available actions for each item?

My question is based on this StackOverflow question: How to modify available actions for each item in a list in Flask-Admin - Stack Overflow

Consider this SQLAlchemy model:

class Record(Base):
    id: Mapped[int]
    attr: Mapped[str]
    can_delete: Mapped[bool]
    can_edit: Mapped[bool]
    can_view_details: Mapped[bool]
    can_do_this: Mapped[bool]  # `@row_action`
    can_do_that: Mapped[bool]  # `@link_row_action`

The situation here is that not every record can be deleted, edited, or viewed. Some items can be deleted yes but some items can not, so what to do here? Disable the "delete" row and batch actions for the entire list or disable it for only relevant records.

hasansezertasan avatar Jan 05 '24 05:01 hasansezertasan

You can override the row-actions.html template for complex use cases

jowilf avatar Jan 17 '24 00:01 jowilf

You can override the row-actions.html template for complex use cases

I tried it but there is no obj in the row-actions.html template. Can you please give an example?

hasansezertasan avatar Jan 27 '24 01:01 hasansezertasan

I tried it but there is no obj in the row-actions.html template. Can you please give an example?

You have access to the request and the primary key (pk).

jowilf avatar Jan 27 '24 20:01 jowilf

I tried it but there is no obj in the row-actions.html template. Can you please give an example?

You have access to the request and the primary key (pk).

Do you have an example in mind? It's a three weeks old issue and I wanted to address it since it'll most probably be a feature on demand.

hasansezertasan avatar Jan 27 '24 21:01 hasansezertasan

I was giving you some hints to implement the solution on StackOverflow.

You can create a custom method in your ModelView:

class MyModelView(ModelView):

    def allow_row_action(self, action, pk)-> bool:
        return True

And in your custom row_actions.html:

{% for action in _actions -%}
        {% if model.allow_row_action(action, pk) %}
         # ...
        {% endif %}
{% endfor %}

jowilf avatar Jan 29 '24 17:01 jowilf

I was giving you some hints to implement the solution on StackOverflow.

You can create a custom method in your ModelView:

class MyModelView(ModelView):

    def allow_row_action(self, action, pk)-> bool:
        return True

And in your custom row_actions.html:

{% for action in _actions -%}
        {% if model.allow_row_action(action, pk) %}
         # ...
        {% endif %}
{% endfor %}

Thank you for the example. I'll check it out ASAP.

hasansezertasan avatar Jan 29 '24 22:01 hasansezertasan