class documentation

class hybrid_property(interfaces.InspectionAttrInfo):

Known subclasses: sqlalchemy.ext.indexable.index_property

View In Hierarchy

A decorator which allows definition of a Python descriptor with both instance-level and class-level behavior.
Method __init__ Create a new .hybrid_property.
Method comparator Provide a modifying decorator that defines a custom comparator producing method.
Method deleter Provide a modifying decorator that defines a deletion method.
Method expression Provide a modifying decorator that defines a SQL-expression producing method.
Method getter Provide a modifying decorator that defines a getter method.
Method setter Provide a modifying decorator that defines a setter method.
Method update​_expression Provide a modifying decorator that defines an UPDATE tuple producing method.
Method __delete__ Undocumented
Method __get__ Undocumented
Method __set__ Undocumented
Method ​_copy Undocumented
Method ​_get​_comparator Undocumented
Method ​_get​_expr Undocumented
Class Variable is​_attribute True if this object is a Python :term:`descriptor`.
Instance Variable custom​_comparator Undocumented
Instance Variable expr Undocumented
Instance Variable fdel Undocumented
Instance Variable fget Undocumented
Instance Variable fset Undocumented
Instance Variable update​_expr Undocumented
Property ​_expr​_comparator Undocumented
Property overrides Prefix for a method that is overriding an existing attribute.

Inherited from InspectionAttrInfo:

Property info Info dictionary associated with the object, allowing user-defined data to be associated with this .InspectionAttr.

Inherited from InspectionAttr (via InspectionAttrInfo):

Class Variable __slots__ Undocumented
Class Variable ​_is​_internal​_proxy True if this object is an internal proxy object.
Class Variable is​_aliased​_class True if this object is an instance of .AliasedClass.
Class Variable is​_bundle True if this object is an instance of .Bundle.
Class Variable is​_clause​_element True if this object is an instance of _expression.ClauseElement.
Class Variable is​_instance True if this object is an instance of .InstanceState.
Class Variable is​_mapper True if this object is an instance of _orm.Mapper.
Class Variable is​_property True if this object is an instance of .MapperProperty.
Class Variable is​_selectable Return True if this object is an instance of _expression.Selectable.
def __init__(self, fget, fset=None, fdel=None, expr=None, custom_comparator=None, update_expr=None):

Create a new .hybrid_property.

Usage is typically via decorator:

from sqlalchemy.ext.hybrid import hybrid_property

class SomeClass(object):
    @hybrid_property
    def value(self):
        return self._value

    @value.setter
    def value(self, value):
        self._value = value
def comparator(self, comparator):

Provide a modifying decorator that defines a custom comparator producing method.

The return value of the decorated method should be an instance of ~.hybrid.Comparator.

Note

The .hybrid_property.comparator decorator replaces the use of the .hybrid_property.expression decorator. They cannot be used together.

When a hybrid is invoked at the class level, the ~.hybrid.Comparator object given here is wrapped inside of a specialized .QueryableAttribute, which is the same kind of object used by the ORM to represent other mapped attributes. The reason for this is so that other class-level attributes such as docstrings and a reference to the hybrid itself may be maintained within the structure that's returned, without any modifications to the original comparator object passed in.

Note

When referring to a hybrid property from an owning class (e.g. SomeClass.some_hybrid), an instance of .QueryableAttribute is returned, representing the expression or comparator object as this hybrid object. However, that object itself has accessors called expression and comparator; so when attempting to override these decorators on a subclass, it may be necessary to qualify it using the .hybrid_property.overrides modifier first. See that modifier for details.

def deleter(self, fdel):
Provide a modifying decorator that defines a deletion method.
def expression(self, expr):

Provide a modifying decorator that defines a SQL-expression producing method.

When a hybrid is invoked at the class level, the SQL expression given here is wrapped inside of a specialized .QueryableAttribute, which is the same kind of object used by the ORM to represent other mapped attributes. The reason for this is so that other class-level attributes such as docstrings and a reference to the hybrid itself may be maintained within the structure that's returned, without any modifications to the original SQL expression passed in.

Note

When referring to a hybrid property from an owning class (e.g. SomeClass.some_hybrid), an instance of .QueryableAttribute is returned, representing the expression or comparator object as well as this hybrid object. However, that object itself has accessors called expression and comparator; so when attempting to override these decorators on a subclass, it may be necessary to qualify it using the .hybrid_property.overrides modifier first. See that modifier for details.

def getter(self, fget):

Provide a modifying decorator that defines a getter method.

New in version 1.2.
def setter(self, fset):
Provide a modifying decorator that defines a setter method.
def update_expression(self, meth):

Provide a modifying decorator that defines an UPDATE tuple producing method.

The method accepts a single value, which is the value to be rendered into the SET clause of an UPDATE statement. The method should then process this value into individual column expressions that fit into the ultimate SET clause, and return them as a sequence of 2-tuples. Each tuple contains a column expression as the key and a value to be rendered.

E.g.:

class Person(Base):
    # ...

    first_name = Column(String)
    last_name = Column(String)

    @hybrid_property
    def fullname(self):
        return first_name + " " + last_name

    @fullname.update_expression
    def fullname(cls, value):
        fname, lname = value.split(" ", 1)
        return [
            (cls.first_name, fname),
            (cls.last_name, lname)
        ]
New in version 1.2.
def __delete__(self, instance):

Undocumented

def __get__(self, instance, owner):

Undocumented

def __set__(self, instance, value):

Undocumented

def _copy(self, **kw):

Undocumented

def _get_comparator(self, comparator):

Undocumented

def _get_expr(self, expr):

Undocumented

is_attribute: bool =

True if this object is a Python :term:`descriptor`.

This can refer to one of many types. Usually a .QueryableAttribute which handles attributes events on behalf of a .MapperProperty. But can also be an extension type such as .AssociationProxy or .hybrid_property. The .InspectionAttr.extension_type will refer to a constant identifying the specific subtype.

See Also

_orm.Mapper.all_orm_descriptors

custom_comparator =

Undocumented

expr =

Undocumented

fdel =

Undocumented

fget =

Undocumented

fset =

Undocumented

update_expr =

Undocumented

@util.memoized_property
_expr_comparator =

Undocumented

@property
overrides =

Prefix for a method that is overriding an existing attribute.

The .hybrid_property.overrides accessor just returns this hybrid object, which when called at the class level from a parent class, will de-reference the "instrumented attribute" normally returned at this level, and allow modifying decorators like .hybrid_property.expression and .hybrid_property.comparator to be used without conflicting with the same-named attributes normally present on the .QueryableAttribute:

class SuperClass(object):
    # ...

    @hybrid_property
    def foobar(self):
        return self._foobar

class SubClass(SuperClass):
    # ...

    @SuperClass.foobar.overrides.expression
    def foobar(cls):
        return func.subfoobar(self._foobar)
New in version 1.2.