4

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

Youpsla
  • 159
  • 2
  • 13

1 Answers1

15

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.

dirn
  • 19,454
  • 5
  • 69
  • 74
  • Thanks a lots for your fast and clear answer. It works like a charm. Just another question. You said you use potisional arguments. I was unable to find where I can find the descirption of positional or named arguments that can be used with ModelView. You have an idea where I can find this doc (I'm using SQLAlchemy)? – Youpsla Feb 25 '14 at 16:14
  • You won't find a description in the Flask-Admin documentation. Positional vs keyword arguments is a [general Python concept](http://docs.python.org/3/glossary.html#term-argument). If you need more, this question has been [asked before](http://stackoverflow.com/questions/1419046/python-normal-arguments-vs-keyword-arguments). – dirn Feb 25 '14 at 16:57
  • Hi again. My question was not about the difference between named and positional args. My issue is that I can't find in the flask-admin doc where I can find a description of positionnal or named args for the 'add_view' method of the ModelView class. (Sry for my english. My first comment was not enough clear.) – Youpsla Feb 25 '14 at 17:10
  • Did you try looking at [the source](https://github.com/mrjoes/flask-admin/blob/master/flask_admin/contrib/sqla/view.py#L243-L244)? – dirn Feb 25 '14 at 17:16
  • Yep that the kind of informations I was looking for. Many, many thanks for this answer. I thought this is the kind of info I can find in the [API doc](http://flask-admin.readthedocs.org/en/latest/api/mod_contrib_sqla/). Do you think this is normal not finding those infos in the generated API doc ? – Youpsla Feb 25 '14 at 17:28
  • 1
    OK. I'm just stupid !! [Link](http://flask-admin.readthedocs.org/en/latest/api/mod_contrib_sqla/) – Youpsla Feb 25 '14 at 23:23