When executing my Flask application I get the following warnings:
C:\Python27\lib\site-packages\flask_admin\model\base.py:1324: UserWarning: Fields missing from ruleset: password
warnings.warn(text)
C:\Python27\lib\site-packages\flask_admin\model\base.py:1324: UserWarning: Fields missing from ruleset: new_password
warnings.warn(text)
* Restarting with stat
C:\Python27\lib\site-packages\flask_admin\model\base.py:1324: UserWarning: Fields missing from ruleset: password
warnings.warn(text)
C:\Python27\lib\site-packages\flask_admin\model\base.py:1324: UserWarning: Fields missing from ruleset: new_password
warnings.warn(text)
I've normally used form_excluded_columns to remove unwanted fields from my form, but this time I have a hard time to get rid of those errors.
Here's my view:
class AdministratorView(sqla.ModelView):
page_size = 10
column_searchable_list = (
'username',
'description'
)
column_list = (
'username',
'apikey',
'description',
'active'
)
column_exclude_list = list = (
'apikey',
'source'
)
form_excluded_columns = (
'source',
'photos'
)
column_labels = {
'apikey': 'API Key'
}
form_widget_args = {
'apikey':{
'readonly':True
}
}
form_create_rules = (
rules.FieldSet(('username', 'password', 'description'), 'Personal'),
rules.FieldSet(('roles', 'apikey', 'active'), 'Permission'),
)
form_edit_rules = (
rules.FieldSet(('username', 'description'), 'Personal'),
rules.FieldSet(('roles', 'apikey', 'active'), 'Permission'),
rules.Header('Reset password'),
rules.Field('new_password')
)
def on_model_change(self, form, model, is_created):
if is_created is False:
if form.new_password.data:
model.password = generate_password_hash(form.new_password.data)
def scaffold_form(self):
form_class = super(AdministratorView, self).scaffold_form()
form_class.password = fields.PasswordField('Password', [validators.Required()])
form_class.new_password = fields.PasswordField('New Password')
return form_class
def is_accessible(self):
if login.current_user.is_authenticated:
return login.current_user.has_role('admin')
The purpose of this view is to have a single, required Password-field on the create form and a optional "New password"-field on the edit-form. I understand that the warnings arise when I don't include password/new_password in form_create_rules and form_edit_rules, but adding those fields to form_excluded_columns doesn't fix it.
Any tips on how I can get rid of the warnings?
Edit:
I suppose I should rather use get_create_form and get_edit_form instead of only scaffold_form. One benefit is that this makes it easier to override each form separately. Can I simplify this further? Should I do requirement validation like this or add nullable=False to the database schema (SQLAlchemy)?
class AdministratorView(sqla.ModelView):
page_size = 10
column_searchable_list = (
'username',
'description'
)
column_list = (
'username',
'apikey',
'description',
'active'
)
column_exclude_list = list = (
'apikey',
'source'
)
form_excluded_columns = (
'source',
'photos'
)
column_labels = {
'apikey': 'API Key'
}
form_widget_args = {
'apikey':{
'readonly':True
}
}
form_create_rules = (
rules.FieldSet(('username', 'password', 'description'), 'Personal'),
rules.FieldSet(('roles', 'apikey', 'active'), 'Permission'),
)
form_edit_rules = (
rules.FieldSet(('username', 'description'), 'Personal'),
rules.FieldSet(('roles', 'apikey', 'active'), 'Permission'),
rules.Header('Reset password'),
rules.Field('new_password')
)
def get_create_form(self):
form = self.scaffold_form()
form.username = fields.StringField('Username', [validators.Required()])
form.password = fields.PasswordField('Password', [validators.Required()])
return form
def get_edit_form(self):
form = self.scaffold_form()
delattr(form, 'password')
form.new_password = fields.PasswordField('New Password')
return form
def on_model_change(self, form, model, is_created):
if is_created is False:
if form.new_password.data:
model.password = generate_password_hash(form.new_password.data)
def is_accessible(self):
if login.current_user.is_authenticated:
return login.current_user.has_role('admin')
答案 0 :(得分:1)
也许这会有所帮助:How can I avoid Flask-Admin 2.1 warning "UserWarning: Fields missing from ruleset"?
以下是该答案的相关代码:
import warnings
with warnings.catch_warnings():
warnings.filterwarnings('ignore', 'Fields missing from ruleset', UserWarning)
admin.add_view(UserView())