namrata wrote:

> I am creating an instance of a class. for example let the class be
> Temp and the attributes of the class be first_name and last_name.
> in order to access the attributes i will usually use the following
> command:
> a = Temp()
> a.last_name = "*******"
> 
> now, i am using sqlalchemy and mapping a table in postgresql to
> python. i am trying to implement a method where i can pass the column
> name and a value to the method. the method will fire a query for that
> column name and value. the query is as follows:
> query = session.query(Temp).query.filter(Temp.last_name == some_value)
> 
> the column name passed to the method is stored in a local variable say
> 'col_name'
> how do i pass this local variable to the query. when i try the below
> query it throws an error saying col_name is not an attirbute
> query = session.query(Temp).query.filter(Temp.col_name == some_value)

I'd guess you can use getattr():

query = session.query(Temp).query.filter(getattr(Temp, col_name) == 
some_value)

Peter
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to