python - SQLAlchemy: Execute/Add a function to a column in sqlalchmey -



python - SQLAlchemy: Execute/Add a function to a column in sqlalchmey -

i'm using sqlalchemy , have query 1 of columns obtain constant quote_status_error, values in column integers. since constant value doesn't mean end-user, i'd convert value within query show string mapping values of column dictionary have in app using function have in place purpose already. haven't been able find way implement since columns in query object not value of column itself. create question clear illustration of have:

query:

q = meta.session.query(mymodel.id, mymodel.quote_status).join(anothermodel).subquery("q")

function want use:

def get_status_names(status_value): homecoming quote_status_names[status_value]

is there way straight sqlalchemy attaching/passing function (get_status_names()) column (mymodel.quote_status). if not best approach? prefer not iterate on values 1 time results in case list of results extensive. appreciate force in right direction.

update: i'm joining resulting subquery other tables

there few things can do.

off top of head...

if want display things, can utilize property decorator: quote_status__id_2_name = {} class mymodel(object): id = column() quote_status_id = column() @property def quote_status_string(self): if self.quote_status_id: homecoming quote_status__id_2_name[self.quote_status_id] homecoming none if want render/accept strings , have sqlalchemy convert string/int transparently, can utilize typedecorator -- http://docs.sqlalchemy.org/en/rel_0_9/core/types.html#custom-types

personally, go property decorator.

python postgresql sqlalchemy

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -