How can I avoid Flask-Admin 2.1 warning "UserWarning: Fields missing from ruleset"?
Asked Answered
M

1

11

I'm using Flask-Admin 2.1 with Python 2.7.6.

One of my Flask-Admin model classes inherits from flask.ext.admin.contrib.sqla.ModelView and overrides form_rules.

When I run my application, this warning is displayed: "UserWarning: Fields missing from ruleset"

The warning is accurate: There are fields in my model that are not included in the ruleset. But that's by design. I don't want those fields to be displayed when users create or edit instances of this model.

I have already read this: https://github.com/flask-admin/flask-admin/pull/815#issuecomment-81963865

How can I suppress the warning?

Midsection answered 4/12, 2015 at 15:32 Comment(1)
Here's why there's a warning - if field is in not in a ruleset, but in the form, WTForms will always delete whatever value there was before. That's how browsers work - if value was not sent, then it is empty. So, if you don't want for field to be present in a form - remove it from the form. If you want it in the form for whatever reason, but don't want to show it - make it hidden.Halflife
K
20

You can suppress the warning when the view is added by using this snippet with the assumed name UserView:

import warnings

with warnings.catch_warnings():
    warnings.filterwarnings('ignore', 'Fields missing from ruleset', UserWarning)
    admin.add_view(UserView())

Reference: https://docs.python.org/2/library/warnings.html#warnings.filterwarnings

Kharkov answered 31/3, 2016 at 13:46 Comment(1)
Great answer! I refined it slightly to catch only the particular warning I was concerned about, by changing warnings.simplefilter('ignore') to warnings.filterwarnings('ignore', 'Fields missing from ruleset').Midsection

© 2022 - 2024 — McMap. All rights reserved.