Reputation: 705
Using WTForms, SQLAlchemy I'm trying to have the user select a Country keyword from a dropdown and from that choice we pass the coordinates (west, south, east, north) of that country back to the program.
Been stuck on how this can be done as the choices=GeoKeywords.label
passes the country fine. Selecting "Albania" passes the value "Albania". But how can I bring in west, south, east, north based on that selection?
Database Table:
GP_DD_GEOKEYWORDS= Table('GP_DD_GEOKEYWORDS', Base.metadata,
Column('VALUE', String(75)),
Column('LABEL', String(75)),
Column('WEST', String(50)),
Column('SOUTH', String(50)),
Column('NORTH', String(50)),
Column('EAST', String(50)))
class GeoKeywords():
s = select([GP_DD_GEOKEYWORDS.c.VALUE, GP_DD_GEOKEYWORDS.c.LABEL])
result = connection.execute(s)
label = [row for row in result]
class ReusableForm(Form):
region = SelectField('Geographic Keyword:', choices=GeoKeywords.label)
@app.route("/editorother", methods=['GET', 'POST'])
@login_required
def editorother():
form = ReusableForm(request.form)
if request.method == 'POST':
region = request.form['region']
if form.validate():
"Do stuff with region and coordinates"
Upvotes: 0
Views: 59
Reputation: 705
@shiv answer is very close and got me in the right direction, I will mark it as the answer though you will need to edit the .where
placement. In the end this worked:
class Coodinator:
def coordinates_query(self):
self.coords_select = select([GP_DD_GEOKEYWORDS.c.WEST,
GP_DD_GEOKEYWORDS.c.SOUTH, GP_DD_GEOKEYWORDS.c.EAST,
GP_DD_GEOKEYWORDS.c.NORTH])
self.coords_select = self.coords_select.where(GP_DD_GEOKEYWORDS.c.LABEL == region)
self.coords_row = connection.execute(self.coords_select)
for row in self.coords_row:
self.coords_row = dict(row)
def coordinates_west(self):
return self.coords_row['WEST']
Upvotes: 0
Reputation: 226
You need to create another query which returns the NORTH, EAST, SOUTH, and WEST columns for the selected region.
...
if form.validate():
coords_query = select([GP_DD_GEOKEYWORDS.c.NORTH,
GP_DD_GEOKEYWORDS.c.EAST,
GP_DD_GEOKEYWORDS.c.SOUTH,
GP_DD_GEOKEYWORDS.c.WEST])
.where(GP_DD_GEOKEYWORDS.c.LABEL == region)
result = connection.execute(coords_query)
# result will be a list of matching rows with coordinates arranged in a tuple taking the same order as the 'select' statement
# e.g. (NORTH, EAST, SOUTH, WEST)
...
Upvotes: 1