I have created an admin panel using Flask-Admin, and created three non admin users. I want to display a database table called as Usertest inside the users section of the panel. Right now for the admin (super user part) I am able to display all the database tables. I want to display one or two tables in the user view of the admin panel, how do i go about it ?
Here is the model_views.py code
class MyModelView(sqla.ModelView):
def is_accessible(self):
if not current_user.is_active or not current_user.is_authenticated:
return False
if current_user.has_role('superuser') :
return True
return False
def _handle_view(self, name, **kwargs):
"""
Override builtin _handle_view in order to redirect users
when a view is not accessible.
"""
if not self.is_accessible():
if current_user.is_authenticated:
# permission denied
abort(403)
else:
# login
return redirect(url_for('security.login', next=request.url))
class CarUserView():
column_display_pk = True
form_columns = ['id', 'desc']
column_searchable_list = ['desc']
column_filters = ['id']
can_create = True
can_edit = True
can_delete = False # disable model deletion
can_view_details = True
page_size = 2 # pagination
create_modal = True
edit_modal = True
can_export = True
class CarAdminView(MyModelView):
column_display_pk = True
form_columns = ['id', 'desc']
column_searchable_list = ['desc']
column_filters = ['id']
column_editable_list = ['desc']
can_create = True
can_edit = True
can_delete = True
can_view_details = True
page_size = 2
create_modal = True
edit_modal = True
can_export = True
and this is the app.py code :
class Car(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
desc = db.Column(db.String(50))
def __str__(self):
return self.desc
class usertest(db.Model):
id=db.Column(db.Integer, primary_key=True, autoincrement=True)
desc=db.Column(db.String(50))
# --------------------------------
# MODEL VIEW CLASSES
# --------------------------------
from model_views import (
CarAdminView, CarUserView, MyModelView
)
# --------------------------------
# FLASK VIEWS / ROUTES
# --------------------------------
@app.route('/')
def index():
return render_template('index.html')
#return redirect("/admin")
# --------------------------------
# CREATE FLASK ADMIN
# --------------------------------
admin = flask_admin.Admin(
app,
'',
base_template='my_master.html',
template_mode='bootstrap3',
)
admin.add_view(CarAdminView(Car, db.session))
admin.add_view(MyModelView(Profile, db.session))
admin.add_view(MyModelView(Username, db.session))
admin.add_view(CarUserView(usertest,db.session))
If i change the conditions in MyModelView such that it displays the table for both admin and non admin user all the tables are being displayed, which is not necessary. I expected it to only display the specified tables for the user view. Please help me out. Thank you.