Code in a class definition is executed at import time, not when the class is instantiated. You need to move the access to session
to the __init__
method so that it will be accessed when creating a form in a view function.
class Institution(Form):
organization = SelectField()
def __init__(self, *args, **kwargs):
self.organization.kwargs['choices'] = [(x, x) for x in session.get('city', ('not set',))]
super().__init__(*args, **kwargs)
This applies to anything that needs an application or request context, such as a database query, not just the session
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…