starlette-admin
starlette-admin copied to clipboard
Question: How do we modify available actions for each item?
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.
You can override the row-actions.html template for complex use cases
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?
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
).
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.
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 %}
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.