12

How can I customize field names in create/edit forms in Flask-Admin?

I know how to change the table name:

class User(db.Model):
    __tablename__ = 'user'

    id = db.Column('user_id', db.Integer, primary_key=True, autoincrement=True)
    first_name = db.Column(db.String(100))
    last_name = db.Column(db.String(100))
    login = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120))
    password = db.Column(db.String(128))    

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

class MyModelView(ModelView):
    def is_accessible(self):
        return current_user.is_authenticated()

admin = Admin(app, u'CustomName')

admin.add_view(MyModelView(User, db.session, u'Custom Table Name'))

In create/edit forms I get field name like name of columns in User db.Model.

How I can simply change this field name?
For example, to get 'Foo' instead of 'login' in create form for a User?

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
Egregors
  • 403
  • 5
  • 10

2 Answers2

21

You are looking for column_labels.

class MyModelView(ModelView):
    column_labels = dict(last_name='CUSTOM LAST NAME')

    def is_accessible(self):
        return current_user.is_authenticated()
iurisilvio
  • 4,868
  • 1
  • 30
  • 36
4

You can:

column_list = ['user.name', 'timestamp']
column_labels = {
'user.name': 'Full Name',
'timestamp' : 'Created Date'
}
awh112
  • 1,466
  • 4
  • 22
  • 34