exclude some models for any users in flask admin
- User
exclude some models for any users in flask admin
- Assistant
To exclude some models for any users in Flask Admin, you can use the
excluded_modelsattribute of theModelViewclass.Here's an example of excluding the
UserandRolemodels from Flask Admin:from flask_admin import Admin from flask_admin.contrib.sqla import ModelView admin = Admin(app) class UserModelView(ModelView): can_create = False can_edit = False can_delete = False class RoleModelView(ModelView): can_create = False can_edit = False can_delete = False admin.add_view(UserModelView(User)) admin.add_view(RoleModelView(Role))In this example, we created custom model views for both the
UserandRolemodels. We set thecan_create,can_edit, andcan_deleteattributes toFalseto restrict any modification to these models by users.Thus, these models will be excluded when any user accesses Flask Admin.
- User
hide some models in flask admin
- Assistant
To hide some models in Flask Admin, you can use the
ModelViewclass and itscan_view_detailsattribute to control the visibility of specific models.Here's an example of hiding the
UserandRolemodels in Flask Admin:from flask_admin import Admin from flask_admin.contrib.sqla import ModelView admin = Admin(app) class UserModelView(ModelView): can_view_details = False class RoleModelView(ModelView): can_view_details = False admin.add_view(UserModelView(User)) admin.add_view(RoleModelView(Role))In this example, we created custom model views for both the
UserandRolemodels. By setting thecan_view_detailsattribute toFalse, we hide the details view for these models. However, the list view will still be accessible.Thus, these models will be hidden in Flask Admin, and users won't be able to view the details of these models.
- User
how to control models to show to different users in flask-admin
- Assistant
To control the models that are shown to different users in Flask Admin, you can create custom views for each user role and restrict the models displayed in each view based on the user's role.
Here's an example of how you can achieve this:
from flask_admin import Admin from flask_admin.contrib.sqla import ModelView admin = Admin(app) class AdminModelView(ModelView): pass class UserModelView(ModelView): can_create = False can_edit = False can_delete = False class RoleModelView(ModelView): can_create = False can_edit = False can_delete = False # Define the users and their roles users = { 'admin': ['User', 'Role'], 'regular_user': ['User'] } # Create custom views for each user role for user, roles in users.items(): views = [] for role in roles: model = globals()[role] view = globals()[f"{role}ModelView"](model) views.append(view) admin.add_view(AdminModelView(name=user, category='Users', menu_icon_type='fa', menu_icon_value='fa-user', menu_icon_color='black', endpoint=user, category_icon_type='fa', category_icon_value='fa-users', category_icon_color='black', category_icon_background_color='white', roles_required=user, category_collapsed=False, menu_collapsed=False, category_visible=True, menu_visible=True, views=views)) - User
how to use is_visible in function in flask admin
- Assistant
To use the
is_visiblefunction in Flask Admin, you can override theis_visiblemethod in your customModelViewclass or create a custom method that returns a boolean value to determine the visibility of a model based on certain conditions.Here's an example of how you can use the
is_visiblefunction in Flask Admin:from flask_admin import Admin from flask_admin.contrib.sqla import ModelView admin = Admin(app) class UserModelView(ModelView): def is_visible(self): # Logic to determine the visibility of the model # For example, show the model only if the current user is an admin return current_user.is_admin admin.add_view(UserModelView(User))In this example, we override the
is_visiblemethod of theUserModelViewto determine whether the model should be visible. We can use any logic necessary to determine the visibility based on our application's requirements. The example shows that the model will only be visible if the current user is an admin. Change the logic inside theis_visiblemethod according to your specific needs.By using the function in this way, you can control the visibility of models in Flask Admin based on different conditions or user roles.