Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Peewee inline models do not get created #1718

Open
forshev opened this issue Oct 4, 2018 · 1 comment
Open

Peewee inline models do not get created #1718

forshev opened this issue Oct 4, 2018 · 1 comment

Comments

@forshev
Copy link

forshev commented Oct 4, 2018

Thanks for your great work on Flask-Admin!

When i add a new inline model instance, then save, the page reloads and nothing happens, no flashes, errors, etc., view's create_model/update_model method is not called so new record is not inserted into the DB.
I presume this is because of required hidden input, which holds id value for existing records but for new records it has no value so form validation error happens.

@cslanziano
Copy link

cslanziano commented Mar 5, 2019

I had a similar problem. As you said the problem is a required hidden input, with name = 'id' that it is empty for new records. After try several things I found a simple solution: set the default value of that field to some number.

class MyModelView(ModelView):
    inline_models = [(db.Model, dict(form_args = dict(id = dict(default = 4))))]

Edit:

The above solution works, but sometimes when try to add a new record in a 'edition view' depending of the default value, it collides with a old record id, then the submit updates the old record and does not create a new one. A solution is use zero as default value to avoid the collision.

class MyModelView(ModelView):
    inline_models = [(db.Model, dict(form_args = dict(id = dict(default = 0))))]

Another solution is set the field as optional using the Optional validator (from wtforms).

from wtforms.validators import Optional

class MyModelView(ModelView):
    inline_models = [(db.Model, dict(form_args = dict(id = dict(validators = [Optional()]))))]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants