NOOOOOOO. Do not abuse eval. there is no need.

Instead you can do:

query = db.aa.forcefield == request.vars.forcefield
if request.vars.color != 'ALL:
    query &= db.aa.color=request.vars.color
if request.vars.weight !='ALL':
    query &= db.aa.weight == request.vars.weigth
query_results = db(query).select()

It is also a little faster.

On Oct 4, 1:09 pm, puercoespin <jzaragoza.puercoes...@gmail.com>
wrote:
> In my app, i build a query for the 'all' value in a drop down form.
>
> For example:
>
> query = '(db.aa.forcefield == request.vars.forcefield)'
>
> if request.vars.color != 'ALL:
>
>     query += ' & (db.aa.color=request.vars.color) '
>
> if request.vars.weight !='ALL':
>
>     query += ' & (db.aa.weight == request.vars.weigth) '
>
> query_results = db(eval(query)).select()
>
> Hope this help...
>
> On 2 oct, 18:30, Will <smits...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Here is my goal. I have  multiple drop down forms populated with the
> > unique values available per field.  I want to add a default ALL value
> > to them.
> > I then want to be able to take those values from the form and do a
> > selection based off those variables.  I can't seem to be able to
> > select all values per given field oddly using the == query.  i have to
> > use the db.field.All selection.  I get there by using if statements,
> > if the variable is equal to ALL.  I have come up with some workable
> > code but it is very ugly and uses multiple if statements and the
> > manual addition of an 'ALL' value in the sets.   I know there has to
> > be a better way.
>
> > So how do I add default ALL selections to drop downs, and how can I
> > do a multiple db selection using those variables.  Here is example
> > code.
> > ##model
> > db.define_table('aa', Field('name'),
> > Field( 'color','string',label ='color'),
> > Field('weight','integer')
> > )
> > ##controller
> > def searchdbanddisplay():
> >     var1 = request.vars.col
> >     var2 = request.vars.weight
> >     ra = db(db.aa.color==var1).select()
> >     wa = db(db.aa.weight==var2).select()
> > ####I want to insert one call to db here instead of the top 2 lines to
> > search for both variables, I am presuming using AND and also
> > incorporate the ALL variable if it is selected?
> >     return dict(message=T('Hello World'),var1 = var1,var2 =
> > var2,ra=ra,wa=wa)
> > def formtry():
>
> >     from sets import Set
> >     ra = db().select(db.aa.color, distinct=True)
> >     SA = Set()
> >     for row in ra:
> >         SA.add(row.color)
> >     ##ra = db().select(db.aa.color, distinct=True)
> >     wa = db().select(db.aa.weight, distinct =True)
> >     WA = Set()
> >     for row in wa:
> >         WA.add(row.weight)
>
> >     ##form = SQLFORM.factory(SQLField('color', label='Select a
> > service'), requires=IS_IN_DB(db,'aa.color'))
> >     form = SQLFORM.factory(
> >     Field('color', requires=IS_IN_SET(SA)),
> >     Field('weight',requires=IS_IN_SET(WA))
> >     )
> >     if form.accepts(request.vars, session):
> >         response.flash = 'form accepted'
> >         redirect(URL('index',vars
> > ={'col' :request.vars.color,'weight' :request.vars.weight}))
> >     return dict(form = form,vabla = session.vars)

Reply via email to