Flask-admin: Defining different fields in edit & create rulesets prints out warning

Created on 9 Aug 2016  路  3Comments  路  Source: flask-admin/flask-admin

The following valid code leads to warnings being printed about missing fields. In my opinion this shouldn't lead to a warning. Is there a way to disable this?

``` python,
class MyModelView(ModelView):
form_columns = ('editonly_field', 'createonly_field', 'id')
form_edit_rules = ('editonly_field', 'id')
form_create_rules = ('createonly_field', 'id')

Output:

flask-admin/flask_admin/model/base.py:1324:
UserWarning: Fields missing from ruleset: createonly_field
```

Most helpful comment

Yes, this is correct behavior.

You defined form with 3 fields, but your display rules omit one of the fields. The browser won't send the field value, wtforms will decide that it is some default value (most likely None) and it'll overwrite whatever you had in the model before.

If you want to show different fields based on the view - override appropriate form methods and contribute fields in the runtime.

I would say that proper way to fix it would be to split form_columns into form_create_columns and form_edit_columns instead. form_*_rules only affect how form is displayed, but don't affect the form itself.

All 3 comments

Yes, this is correct behavior.

You defined form with 3 fields, but your display rules omit one of the fields. The browser won't send the field value, wtforms will decide that it is some default value (most likely None) and it'll overwrite whatever you had in the model before.

If you want to show different fields based on the view - override appropriate form methods and contribute fields in the runtime.

I would say that proper way to fix it would be to split form_columns into form_create_columns and form_edit_columns instead. form_*_rules only affect how form is displayed, but don't affect the form itself.

What is the cleanest way to introduce the concept of form_create_columns / form_edit_columns ?

Is there a way to display the field as a hidden form element, so the proper value is retained? That would fit the model of only controlling how it is displayed, not whether it is displayed, but get the intended job done, of not allowing changes. (Yes, it trusts the client, but this kind of decision, at least in my case, is about streamlining the UI, not security.)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pmazurek picture pmazurek  路  6Comments

nMustaki picture nMustaki  路  3Comments

citizen-stig picture citizen-stig  路  5Comments

macfire picture macfire  路  3Comments

galuszkak picture galuszkak  路  3Comments