根据用户选择从列中获取值

时间:2016-12-15 21:56:02

标签: python python-2.7 flask sqlalchemy flask-wtforms

使用WTForms,SQLAlchemy我试图让用户从下拉列表中选择 Country 关键字,然后从该选项中我们将该国家/地区的坐标(西,南,东,北)传回到该计划。

由于choices=GeoKeywords.label让国家好转,所以一直坚持如何做到这一点。选择“阿尔巴尼亚”通过值“阿尔巴尼亚”。但是,我如何根据这一选择引进西部,南部,东部,北部?

数据库表:

enter image description here

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"

2 个答案:

答案 0 :(得分:1)

您需要创建另一个查询,该查询返回所选区域的NORTH,EAST,SOUTH和WEST列。

...
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)
...

答案 1 :(得分:0)

@shiv的回答非常接近并让我朝着正确的方向前进,我会将其标记为答案,但您需要修改 <script> $( document ).ready(function() { AmnPy.loadModal(); }); </script> 展示位置。最终这有效:

.where