简体   繁体   中英

Flask-Admin : Customize table name for the admin interface

I like to change the name display at the top level menu of Flask-Admin. By default it seems he uses the calass model name. I like to change that for a more Human readable Name.

I've seen that in the "layout.htlm" template, there is a variable called {{ item.name }} . This is used to display the name of the menu. Is there a way to change that for another name or to surcharge the name in the model definition ?

Using __tablename__ doesn't work and will break internal flask-admin. Is there an equivalent to __repr__ but for table name instead of column.

Regards

BaseModelView accepts an argument named name . If you provide a value, it will be used for display on the menu.

from flask.ext.admin.model import BaseModelView

admin.add_view(BaseModelView(MyModel, 'Menu Text'))

If you are using SQLAlchemy:

from flask.ext.admin.contrib.sqla import ModelView

admin.add_view(ModelView(MyModel, db.session, 'Menu Text'))

Here I've used positional arguments, but I could have just as easily done name='Menu Text' instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM