Re: [PythonReports-users] SQLAlchemy optional relationships - what is recommended way to handle
Brought to you by:
a1s
From: alexander s. <al...@go...> - 2012-12-06 07:11:17
|
On 05.12.2012 22:24, Werner F. Bruhin wrote: > > Now to my question I define my fields like this: > > "drinkinfo.subregion_lv.name" and my "data" contains a few entries of > type "cellarbook" each of them has "drinkinfo" relation and it I have > things like "country_lv", "subregion_lv" etc etc, some of them are > optional, i.e. not present on all rows. How should that be defined in > the "expr"? There are at least two ways, depending on your goal: 1. Add "printwhen" condition to the style of your field. 2. Use conditional expression, something like "drinkinfo.subregion_lv.name if 'lv' in drinkinfo.subregions else '--'" > Could I call a method of my own, something along these lines: > > def getRelInfo(objval, sainst): > """Get related information > > :param objval: e.g. "drinkinfo.someoptionalrel.name" > :param sainst: an SA Instance of the row, e.g. a "cellarbook" entry/row > :return: the column value or blank > > """ That is not a method, that's function. To call a (non-builtin) function you have to import module defining that function. See http://pythonreports.sourceforge.net/prt.shtml#import And yes, you should be able to call any method of a data object. Please see SQLAlchemy documentation if you need custom method on ORM objects. Best wishes, alex. |