在 flask-admin 的 ModelView 中为表单自定义小部件

Customize widget for a form in ModelView in flask-admin

我有一个模型News:

class News(db.Model):
    __tablename__ = 'news'
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.String)
    active_from = db.Column(db.DateTime)
    active_until = db.Column(db.DateTime)

像这样集成到 flask-admin 中:

class MyModelView(ModelView):
    def is_accessible(self):
        return current_user.usergroup.name == 'admin'

admin.add_view(MyModelView(News, db.session))

但是当我打开管理页面时,我看到 news.contentinput type='text' 小部件。我怎样才能把 textarea 放在那里呢?

在代码中,db.String列分别是mapped to StringFields, and db.Text columns are mapped to TextAreaFields, which present the user with text inputs and textareas

要覆盖此行为,您可以设置 form_overrides 字段:

from wtforms.fields import TextAreaField

class MyModelView(ModelView):
    form_overrides = dict(string=TextAreaField)
    def is_accessible(self):
        return current_user.usergroup.name == 'admin'