Custom name of field in Flask-Admin
Asked Answered
A

2

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?

Apophyge answered 19/9, 2014 at 13:30 Comment(0)
P
20

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()
Pilgrim answered 19/9, 2014 at 13:44 Comment(1)
@phantom never used it with mongoengine, maybe it is a bug or not implemented.Pilgrim
W
5

You can:

column_list = ['user.name', 'timestamp']
column_labels = {
'user.name': 'Full Name',
'timestamp' : 'Created Date'
}
Whisper answered 26/6, 2018 at 16:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.