Larry Martell <larry.mart...@gmail.com> writes:

> I am trying to write a function that takes kwargs as a param and
> generates an update statement where the rows to be updated are
> specified in an in clause.
>
> Something like this:
>
>     def update_by_in(self, **kwargs):
>         filter_group = []
>         for col in kwargs['query_params']:
>             # obviously this line does not work as col is a string,
> but this is the intent
>             filter_group.append(col.in_(tuple(kwargs['query_params'][col])))
>
>         
> self._session.query(self.model_class).filter(*filter_group).update(kwargs['values'])
>
> self.update_by_in(
>     **{'query_params': {'companyCode': ['A', 'B', 'C']},
>         'values': {'portfolioName': 'test'}}
>  )
>
> Is there a way to do this?

Sure, and easy enough: replace the line where you append to `filter_group`
with something like

  attr = getattr(self.model_class, col)
  filter_group.append(attr.in_(tuple(kwargs['query_params'][col])))

that is, obtain the mapped class member named after "col", and use that to
build the filter expression.

ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
l...@metapensiero.it  |                 -- Fortunato Depero, 1929.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/87tuk5loy1.fsf%40metapensiero.it.

Reply via email to