stamaimer
stamaimer

Reputation: 6475

How to set the fixed value for the create/edit form field in Flask-Admin

I'm using Flask-Admin and Flask-Security to write a website. I want to implement a function: The user which has admin role only can create user which is foo role. The user which has foo role only can create user which is bar role.

I have tried the QueryAjaxModelLoader:

form_ajax_refs = {
    roles: QueryAjaxModelLoader("role", db.session, Role, fileds=["name"])

and form_choices:

form_choices = {"roles": [(3, "foo"), ]}

These two methods don't solve the problem. Can anyone give me some advice?

Upvotes: 0

Views: 788

Answers (1)

stamaimer
stamaimer

Reputation: 6475

At first, I use on_model_change to set the default value of the roles field.

And then, I use form_args and query_factory to limit the choices of roles field.

def filter_func():
    return db.session.query(Role).filter_by(name="applicant")

form_args = {
    "roles": {
        "query_factory": filter_func
    }
}

Upvotes: 2

Related Questions