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

How to use current_user in model_form #290

Open
spitzbubchen opened this issue Dec 13, 2016 · 3 comments
Open

How to use current_user in model_form #290

spitzbubchen opened this issue Dec 13, 2016 · 3 comments
Labels

Comments

@spitzbubchen
Copy link

spitzbubchen commented Dec 13, 2016

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?

@wojcikstefan
Copy link
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
Copy link
Author

spitzbubchen commented Dec 16, 2016

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
Copy link
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
Labels
Projects
None yet
Development

No branches or pull requests

3 participants