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

QuerySelectField's query_factory callable not used when assigned through model_form's field_args #30

Open
bglogic opened this issue Aug 18, 2021 · 0 comments

Comments

@bglogic
Copy link

bglogic commented Aug 18, 2021

Background

In the example below, assuming parent is a many-to-one field that maps to a QuerySelectField, The query_factory's callable (get_all_except_first) will not be used, it just returns all records of the model. On the other hand, using a normal form works as expected.

def get_all_except_first():
    return Example.query.filter(Example.id != 1)

# This doesn't work
ExampleForm = model_form(
    Example,
    field_args={
        "parent": {"query_factory": get_all_except_first}
        ...
    }
    ...
)

# This works
class ExampleForm(FlaskForm):
    parent = QuerySelectField(query_factory=get_all_except_first)
    ...

Failed debugging attempts

Got confused when attempting to debug code similar to the example above (using model_form):

  1. Printing field_args in the conv_ManyToOne method shows that query_factory points to <function ModelConverterBase.convert.<locals>.<lambda> at 0x1076a3b80>.
  2. Using the debugger to inspect field_args in the conv_ManyToOne method shows that it doesn't contain query_factory

https://github.com/wtforms/wtforms-sqlalchemy/blob/9884ab82ffd51a14dc2778468d428985d4c8e26b/wtforms_sqlalchemy/orm.py#L232-L234

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

No branches or pull requests

1 participant