What you are doing is not going to work because your query is not evaluated when you call it within render_template. Instead, the sql query string is sent as is to the template.
You can use a custom decorator since only want to do this for some views/pages.
def get_pending_users(f):
@wraps(f)
def decorated_function(*args, **kwargs):
pending_users = user.company.users.filter(User.account_approved == False).all()
if g.pending_users is None:
g.pending_users = pending_users
return f(*args, **kwargs)
return decorated_function
In your view, you can call it as:
@app.route('/',methods = ['GET','POST])
@get_pending_users
def whatever():
return render_template('templates/dashboard.html')
Then, in your template, just call g.pending_users
{{ g.pending_users }}