在 Flask-admin form_create 中隐藏 ModelView 的字段

Hide fields from ModelView in Flask-admin form_create

是否有适当的方法来排除字段不显示在 Flask-admin 的创建表单中?我通常会做这样的事情来指定要在创建和编辑表单上显示的字段:

class UserView(sqla.ModelView):
    form_create_rules = { 'username' }
    form_edit_rules = ('username', 'photos')

即使这按预期工作,我在 运行 我的应用程序时收到以下警告:

UserWarning: Fields missing from ruleset: photos
  warnings.warn(text)

有没有更好的方法来定义在每个表单上显示哪些字段,并且不会出现该错误?

如果我没理解错的话,你可以使用ModelView的属性class:

form_columns - Collection of the model field names for the form. If set to None will get them from the model.

form_excluded_columns - Collection of the model field names for the form. If set to None will get them from the model.

您可以在 Flask-Admin Docs 中找到更多信息。

我还应该提到,这对 CreateEdit 表单都有效。