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)
评论列表
文章目录