Skip to content

How to use current_user in model_form #290

@spitzbubchen

Description

@spitzbubchen

I need to filter a select menu showing only groups in a user's company. With sqlalchemy there was the QuerySelectField, however, this does not seem to be available in flask-mongoengine. Ideally, I would like to pass flask_login current_user to model_form as follows:

wtfUser = model_form(User, wtf.Form, exclude=[ 'password'], field_args = {
	'group' : { 'queryset': Group.objects(company = current_user.group.company) },
	'roles' : { 'queryset': Role.objects(name__ne = 'admin').order_by('name') }
})

However, since this places current_user outside the request context this results in errors. Does anyone have a clever idea on how to do this?

Activity

wojcikstefan

wojcikstefan commented on Dec 13, 2016

@wojcikstefan
Member

Hm, can "queryset" accept a function and execute it during runtime vs during the definition of the model form? If not, I guess you'll have to construct those querysets at the time of the request.

spitzbubchen

spitzbubchen commented on Dec 16, 2016

@spitzbubchen
Author

Not really sure as I can find very little documentation on "queryset". I have also posted this on stackoverflow. This should be maybe relabeled as a question.

spitzbubchen

spitzbubchen commented on Aug 20, 2017

@spitzbubchen
Author

Here is my solution on stackoverflow. If you feel this can't be addressed in wtforms please feel free to close.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @spitzbubchen@wojcikstefan@lafrech

        Issue actions

          How to use current_user in model_form · Issue #290 · MongoEngine/flask-mongoengine