如何在 Flask Admin 的编辑表单中添加额外字段?
How to add an extra field in the edit form in Flask Admin?
我想使用 Flask Admin 将字段 projects 添加到我的用户编辑表单。
为此,我尝试覆盖 edit_form 方法以将字段作为附加属性添加到表单中,但未添加该字段。
我找不到任何关于如何在 Flask Admin 中向表单添加字段的明确文档。
有人可以帮忙吗?
class UserAdminView(ModelView):
column_exclude_list = ['password',]
form_widget_args = {
'password':{
'disabled': True
}
}
def edit_form(self, obj=None):
form = super(UserAdminView, self).edit_form(obj)
form.projects = sqla.fields.QuerySelectMultipleField(
"Projects",
query_factory=lambda: db.session.query(Project).join(UserProjectRel) \
.filter(UserProjectRel.user_id==obj.id),
)
return form
class User(db.Model):
__tablename__ = 'phaunos_user'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password = db.Column(db.String(255), nullable=False)
is_admin = db.Column(db.Boolean, default=False, nullable=False)
confirmed_on = db.Column(db.DateTime, nullable=True)
class UserProjectRel(db.Model):
__tablename__ = 'user_project_rel'
user_id = db.Column(db.Integer, db.ForeignKey('phaunos_user.id'), primary_key=True)
project_id = db.Column(db.Integer, db.ForeignKey('project.id'), primary_key=True)
user_role = db.Column(ENUM(Role), nullable=False)
user = db.relationship('User', backref=db.backref('user_project_rel', cascade='all'))
project = db.relationship('Project', backref=db.backref('user_project_rel', cascade='all'))
我首先不得不覆盖 get_edit_form:
class UserAdminView(ModelView):
column_exclude_list = ['password',]
form_excluded_columns = ['annotations', 'user_project_rel']
form_widget_args = {
'password':{
'disabled': True
}
}
def get_edit_form(self):
form = super(UserAdminView, self).get_edit_form()
form.projects = sqla.fields.QuerySelectMultipleField("Projects")
return form
def edit_form(self, obj=None):
form = super(UserAdminView, self).edit_form(obj)
form.projects.query = db.session.query(Project).join(UserProjectRel) \
.filter(UserProjectRel.user_id==obj.id).all()
form.projects.data = form.projects.query
return form
我认为您错过了 BaseModelView
class 上的 form_extra_fields
属性,因此要添加 project
输入:
from wtforms.fields import TextField
class UserAdminView(ModelView):
...
form_extra_fields = {
'Projects': TextField('Projects')
}
我想使用 Flask Admin 将字段 projects 添加到我的用户编辑表单。
为此,我尝试覆盖 edit_form 方法以将字段作为附加属性添加到表单中,但未添加该字段。
我找不到任何关于如何在 Flask Admin 中向表单添加字段的明确文档。
有人可以帮忙吗?
class UserAdminView(ModelView):
column_exclude_list = ['password',]
form_widget_args = {
'password':{
'disabled': True
}
}
def edit_form(self, obj=None):
form = super(UserAdminView, self).edit_form(obj)
form.projects = sqla.fields.QuerySelectMultipleField(
"Projects",
query_factory=lambda: db.session.query(Project).join(UserProjectRel) \
.filter(UserProjectRel.user_id==obj.id),
)
return form
class User(db.Model):
__tablename__ = 'phaunos_user'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password = db.Column(db.String(255), nullable=False)
is_admin = db.Column(db.Boolean, default=False, nullable=False)
confirmed_on = db.Column(db.DateTime, nullable=True)
class UserProjectRel(db.Model):
__tablename__ = 'user_project_rel'
user_id = db.Column(db.Integer, db.ForeignKey('phaunos_user.id'), primary_key=True)
project_id = db.Column(db.Integer, db.ForeignKey('project.id'), primary_key=True)
user_role = db.Column(ENUM(Role), nullable=False)
user = db.relationship('User', backref=db.backref('user_project_rel', cascade='all'))
project = db.relationship('Project', backref=db.backref('user_project_rel', cascade='all'))
我首先不得不覆盖 get_edit_form:
class UserAdminView(ModelView):
column_exclude_list = ['password',]
form_excluded_columns = ['annotations', 'user_project_rel']
form_widget_args = {
'password':{
'disabled': True
}
}
def get_edit_form(self):
form = super(UserAdminView, self).get_edit_form()
form.projects = sqla.fields.QuerySelectMultipleField("Projects")
return form
def edit_form(self, obj=None):
form = super(UserAdminView, self).edit_form(obj)
form.projects.query = db.session.query(Project).join(UserProjectRel) \
.filter(UserProjectRel.user_id==obj.id).all()
form.projects.data = form.projects.query
return form
我认为您错过了 BaseModelView
class 上的 form_extra_fields
属性,因此要添加 project
输入:
from wtforms.fields import TextField
class UserAdminView(ModelView):
...
form_extra_fields = {
'Projects': TextField('Projects')
}