SQLAlchemy column passed as dropdown list in WTForm Flask

Question:

I want to take the values of a column named hotelName from Hotel model and populate with them a dropdown list.

I have tried this as a way to put them in my wtform:

class RoomForm(FlaskForm):
    hotel = SelectField(choices=[(h, h) for h in Hotel.hotelName.property.columns[0].type

But it gives error: TypeError: ‘String’ object is not iterable.

I have no idea how to make Hotel.hotelName.property.columns[0].type into something iterable and have it passed in my form.
Or maybe there is altogether a different way to achieve this, but I just can’t find it.

I should mention that I am quite new to Flask so I still get lost in the simplest issues…

Asked By: Maria

||

Answers:

The solution that worked for me is like this.
In Hotel model I added this repr function:

def __repr__(self):
    return self.hotelName

Then had this in my RoomForm:

hotelList = SelectField('Select Hotel', validators=(validators.InputRequired(),))


def __init__(self, *args, **kwargs):
    super(RoomForm, self).__init__(*args, **kwargs)
    self.hotelList.choices = [(interval.id, interval.hotelName) 
                                        for interval in Hotel.query.all()]
Answered By: Maria