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?

Steve Saporta
  • 4,581
  • 3
  • 30
  • 32
  • 3
    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. – Joes Dec 21 '15 at 19:15

1 Answers1

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

mikl
  • 216
  • 3
  • 6
  • 4
    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'). – Steve Saporta Apr 15 '16 at 13:53