Change option label in WTForms-SQLAlchemy QuerySelectField
Solution 1:
add a model object’s __str__
or __unicode__
to your model and set no get_label='name'
for example: in your db-model:
class Platform(db.Model):
name = db.Column(db.String(64), primary_key=True)
platform = db.Column(db.String(64))
def __repr__(self):
return '{} {}'.format(self.name, self.platform)
and in your form:
platform = QuerySelectField('Platform', validators=[DataRequired()], query_factory=GetPlatforms)
Also see the doc:
get_label – If a string, use this attribute on the model class as the label associated with each option. If a one-argument callable, this callable will be passed model instance and expected to return the label text. Otherwise, the model object’s str or unicode will be used.