diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2018-10-30 12:53:42 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2018-10-30 12:53:42 -0400 |
| commit | 65af4fc74ab7f8af4d4eb8d1fdd7aa751731cbfb (patch) | |
| tree | 677c025ad5e9b68d21b355b6acfdc30e64304747 /lib/sqlalchemy | |
| parent | 64c3c81629fbaf07a2d2ce843155f48b40fd64d8 (diff) | |
| parent | 681bd7eb8876c8b83912b8307b2fc7500a11e6f0 (diff) | |
| download | sqlalchemy-65af4fc74ab7f8af4d4eb8d1fdd7aa751731cbfb.tar.gz | |
Merge remote-tracking branch 'origin/pr/478'
Change-Id: I0f27b6426dbfd665edf2a119b1cfaadb54511dd0
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/ext/hybrid.py | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/lib/sqlalchemy/ext/hybrid.py b/lib/sqlalchemy/ext/hybrid.py index 895d8af9d..95eecb93f 100644 --- a/lib/sqlalchemy/ext/hybrid.py +++ b/lib/sqlalchemy/ext/hybrid.py @@ -118,6 +118,8 @@ SQL expression-level boolean behavior:: OR interval.start <= interval_1."end" AND interval."end" > interval_1."end" +.. _hybrid_distinct_expression: + Defining Expression Behavior Distinct from Attribute Behavior -------------------------------------------------------------- @@ -157,6 +159,28 @@ object for class-level expressions:: FROM interval WHERE abs(interval."end" - interval.start) / :abs_1 > :param_1 +.. note:: When defining an expression for a hybrid property or method, the + expression method **must** retain the name of the original hybrid, else + the new hybrid with the additional state will be attached to the class + with the non-matching name. To use the example above:: + + class Interval(object): + # ... + + @hybrid_property + def radius(self): + return abs(self.length) / 2 + + # WRONG - the non-matching name will cause this function to be + # ignored + @radius.expression + def radius_expression(cls): + return func.abs(cls.length) / 2 + + This is also true for other mutator methods, such as + :meth:`.hybrid_property.update_expression`. This is the same behavior + as that of the ``@property`` construct that is part of standard Python. + Defining Setters ---------------- @@ -202,7 +226,7 @@ expression is used as the column that's the target of the SET. If our However, when using a composite hybrid like ``Interval.length``, this hybrid represents more than one column. We can set up a handler that will accommodate a value passed to :meth:`.Query.update` which can affect -this, using the :meth:`.hybrid_propery.update_expression` decorator. +this, using the :meth:`.hybrid_property.update_expression` decorator. A handler that works similarly to our setter would be:: class Interval(object): @@ -965,6 +989,10 @@ class hybrid_property(interfaces.InspectionAttrInfo): :attr:`.hybrid_property.overrides` modifier first. See that modifier for details. + .. seealso:: + + :ref:`hybrid_distinct_expression` + """ return self._copy(expr=expr) |
