Created
September 30, 2021 05:52
-
-
Save WorldException/c7283f7546fb54ec7df5c7708bfc47c8 to your computer and use it in GitHub Desktop.
SelectFieldDynamic use function for choices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SelectFieldDynamic(SelectField): | |
# use function in choices | |
def __init__(self, label=None, validators=None, coerce=str, choices=None, validate_choice=True, **kwargs): | |
super(SelectFieldDynamic, self).__init__(label, validators, **kwargs) | |
self.coerce = coerce | |
self.choices = choices | |
self.validate_choice = validate_choice | |
def iter_choices(self): | |
if not self.choices: | |
choices = [] | |
elif callable(self.choices): | |
choices = self.choices() | |
elif isinstance(self.choices[0], (list, tuple)): | |
choices = self.choices | |
else: | |
choices = zip(self.choices, self.choices) | |
for value, label in choices: | |
yield (value, label, self.coerce(value) == self.data) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment