我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用wtforms.fields.SelectField()。
def conv_Enum(self, column, field_args, **extra): if 'choices' not in field_args: field_args['choices'] = [(e, e) for e in column.type.enums] return f.SelectField(**field_args)
def convert(self, model, field, field_args): kwargs = { 'label': field.verbose_name, 'description': field.help_text, 'validators': [], 'filters': [], 'default': field.default, } if field_args: kwargs.update(field_args) if field.blank: kwargs['validators'].append(validators.Optional()) if field.max_length is not None and field.max_length > 0: kwargs['validators'].append(validators.Length(max=field.max_length)) ftype = type(field).__name__ if field.choices: kwargs['choices'] = field.choices return f.SelectField(**kwargs) elif ftype in self.converters: return self.converters[ftype](model, field, kwargs) else: converter = getattr(self, 'conv_%s' % ftype, None) if converter is not None: return converter(model, field, kwargs)
def conv_NullBooleanField(self, model, field, kwargs): from django.db.models.fields import NOT_PROVIDED def coerce_nullbool(value): d = {'None': None, None: None, 'True': True, 'False': False} if isinstance(value, NOT_PROVIDED): return None elif value in d: return d[value] else: return bool(int(value)) choices = ((None, 'Unknown'), (True, 'Yes'), (False, 'No')) return f.SelectField(choices=choices, coerce=coerce_nullbool, **kwargs)
def convert(self, model, prop, field_args): """ Returns a form field for a single model property. :param model: The ``db.Model`` class that contains the property. :param prop: The model property: a ``db.Property`` instance. :param field_args: Optional keyword arguments to construct the field. """ prop_type_name = type(prop).__name__ kwargs = { 'label': prop.name.replace('_', ' ').title(), 'default': prop.default_value(), 'validators': [], } if field_args: kwargs.update(field_args) if prop.required and prop_type_name not in self.NO_AUTO_REQUIRED: kwargs['validators'].append(validators.required()) if prop.choices: # Use choices in a select field if it was not provided in field_args if 'choices' not in kwargs: kwargs['choices'] = [(v, v) for v in prop.choices] return f.SelectField(**kwargs) else: converter = self.converters.get(prop_type_name, None) if converter is not None: return converter(model, prop, kwargs)
def conv_Enum(self, column, field_args, **extra): field_args['choices'] = [(e, e) for e in column.type.enums] return f.SelectField(**field_args)
def conv_Enum(self, column, field_args, **extra): field_args['choices'] = [(e, e) for e in column.type.enums] return wtforms_fields.SelectField(**field_args)
def get_params_data(form_instance): result = [] type_mapping = \ { 'hudson.model.StringParameterDefinition': ('StringField', {}) , 'hudson.model.TextParameterDefinition': ('StringField', {}) , 'hudson.model.BooleanParameterDefinition': ('BooleanField', {}) , 'hudson.model.ChoiceParameterDefinition': ('SelectField', {}) } xml_config = get_config(form_instance) if not xml_config: return result tree = etree.fromstring(xml_config) params_root = tree.findall('properties/hudson.model.ParametersDefinitionProperty/parameterDefinitions') if params_root: params = params_root[0].getchildren() for param in params: name = getattr(param.find('name'), 'text', None) if not name: continue field_type, kwargs = type_mapping.get(param.tag, None) if not type: continue choices = [(node.text, node.text) for node in param.findall('choices/a/string')] if choices: choices = [('', u'????????'), ] + choices result.append\ ( { 'name': name , 'type': field_type , 'description': getattr(param.find('description'), 'text', None) , 'default': getattr(param.find('defaultValue'), 'text', None) , 'choices': choices , 'kwargs': kwargs } ) return result
def jenkins_form_factory(*args, **kwargs): attributes = dict\ ( signature = fields.HiddenField\ () , view = fields.SelectField\ ( u'???' , choices=JenkinsViewChoices() , default=None ) , parent = InlineFieldList\ ( InlineFormField ( ParentForm ) , label=u'????????' , description=u'??????? ???????????? ?????' , validators= [ validators.DataRequired() , ] ) , node = fields.SelectField\ ( u'????' , choices=JenkinsNodeChoices() , validators= [ validators.DataRequired() , ] ) , config = fields.StringField\ ( u'???????? ??????' , widget=ReadonlyTextArea() ) ) form_instance = type('Form', (form.Form, ), attributes)(*args, **kwargs) if form_instance.parent.data: attributes['param'] = InlineFormField\ ( jenkins_param_form_factory(form_instance) , u'?????????' ) kwargs['csrf_enabled'] = False return type('Form', (form.Form, ), attributes)(*args, **kwargs)