简体   繁体   中英

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?

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM