Changeset 382
- Timestamp:
- 07/29/08 10:48:03 (4 years ago)
- Location:
- elixir/trunk
- Files:
-
- 2 modified
-
CHANGES (modified) (1 diff)
-
elixir/properties.py (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
elixir/trunk/CHANGES
r380 r382 6 6 resorting to using the ugly "column_kwargs" (patch from Jason R. Coombs, 7 7 closes #39). 8 - extra args and kwargs to Synonym and ColumnProperty are forwarded to their 9 underlying constructs 8 10 9 11 -
elixir/trunk/elixir/properties.py
r362 r382 67 67 def create_properties(self): 68 68 ''' 69 Subclasses may override this method to add properties to the involved 69 Subclasses may override this method to add properties to the involved 70 70 entity. 71 71 ''' … … 128 128 129 129 # delete the original attribute so that it doesn't interfere with 130 # SQLAlchemy. 130 # SQLAlchemy. Note that getattr and delattr are not symmetrical: 131 # getattr look up in parent classes, while delattr must be called on 132 # the exact class holding the attribute. 131 133 if name in entity.__dict__: 132 134 delattr(entity, name) … … 149 151 ''' 150 152 151 def __init__(self, prop ):152 super(GenericProperty, self).__init__( )153 def __init__(self, prop, *args, **kwargs): 154 super(GenericProperty, self).__init__(*args, **kwargs) 153 155 self.prop = prop 156 self.args = args 157 self.kwargs = kwargs 154 158 155 159 def create_properties(self): … … 162 166 163 167 def evaluate_property(self, prop): 168 if self.args or self.kwargs: 169 raise Exception('superfluous arguments passed to GenericProperty') 164 170 return prop 165 171 … … 170 176 ``column_property``'s. 171 177 172 It takes a single argument, which is a function (often 173 given as an anonymous lambda) taking one argument and returning the 174 desired (scalar-returning) SQLAlchemy ClauseElement. That function will be 175 called whenever the entity table is completely defined, and will be given 176 the .c attribute of the entity as argument (as a way to access the entity 177 columns). The ColumnProperty will first wrap your ClauseElement in a label 178 with the same name as the property, then wrap that in a column_property. 178 It takes a function (often given as an anonymous lambda) as its first 179 argument. Other arguments and keyword arguments are forwarded to the 180 column_property construct. That first-argument function must accept exactly 181 one argument and must return the desired (scalar-returning) SQLAlchemy 182 ClauseElement. 183 184 The function will be called whenever the entity table is completely 185 defined, and will be given 186 the .c attribute of the table of the entity as argument (as a way to 187 access the entity columns). The ColumnProperty will first wrap your 188 ClauseElement in an 189 "empty" label (ie it will be labelled automatically during queries), 190 then wrap that in a column_property. 179 191 180 192 .. sourcecode:: python … … 183 195 quantity = Field(Float) 184 196 unit_price = Field(Numeric) 185 price = ColumnProperty(lambda c: c.quantity * c.unit_price) 197 price = ColumnProperty(lambda c: c.quantity * c.unit_price, 198 deferred=True) 186 199 187 200 Please look at the `corresponding SQLAlchemy … … 191 204 192 205 def evaluate_property(self, prop): 193 return column_property(prop.label(None) )206 return column_property(prop.label(None), *self.args, **self.kwargs) 194 207 195 208 … … 216 229 217 230 def evaluate_property(self, prop): 218 return synonym(prop )231 return synonym(prop, *self.args, **self.kwargs) 219 232 220 233 #class Composite(GenericProperty):
