當前位置: 首頁>>代碼示例>>Python>>正文


Python attributes.QueryableAttribute方法代碼示例

本文整理匯總了Python中sqlalchemy.orm.attributes.QueryableAttribute方法的典型用法代碼示例。如果您正苦於以下問題:Python attributes.QueryableAttribute方法的具體用法?Python attributes.QueryableAttribute怎麽用?Python attributes.QueryableAttribute使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在sqlalchemy.orm.attributes的用法示例。


在下文中一共展示了attributes.QueryableAttribute方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: get_descriptor_columns

# 需要導入模塊: from sqlalchemy.orm import attributes [as 別名]
# 或者: from sqlalchemy.orm.attributes import QueryableAttribute [as 別名]
def get_descriptor_columns(model, descriptor):
    if isinstance(descriptor, InstrumentedAttribute):
        return descriptor.property.columns
    elif isinstance(descriptor, sa.orm.ColumnProperty):
        return descriptor.columns
    elif isinstance(descriptor, sa.Column):
        return [descriptor]
    elif isinstance(descriptor, sa.sql.expression.ClauseElement):
        return []
    elif isinstance(descriptor, sa.ext.hybrid.hybrid_property):
        expr = descriptor.expr(model)
        try:
            return get_descriptor_columns(model, expr)
        except TypeError:
            return []
    elif (
        isinstance(descriptor, QueryableAttribute) and
        hasattr(descriptor, 'original_property')
    ):
        return get_descriptor_columns(model, descriptor.property)
    raise TypeError(
        'Given descriptor is not of type InstrumentedAttribute, '
        'ColumnProperty or Column.'
    ) 
開發者ID:kvesteri,項目名稱:sqlalchemy-json-api,代碼行數:26,代碼來源:utils.py

示例2: to_json

# 需要導入模塊: from sqlalchemy.orm import attributes [as 別名]
# 或者: from sqlalchemy.orm.attributes import QueryableAttribute [as 別名]
def to_json(self):
        """Exports the object to a JSON friendly dict

        Returns:
             Dict representation of object type
        """
        output = {
            '__type': self.__class__.__name__
        }

        cls_attribs = self.__class__.__dict__
        for attrName in cls_attribs:
            attr = getattr(self.__class__, attrName)
            value = getattr(self, attrName)
            value_class = type(value)

            if issubclass(type(attr), QueryableAttribute):
                # List of Model, BaseModelMixin objects (one-to-many relationship)
                if issubclass(value_class, InstrumentedList):
                    output[to_camelcase(attrName)] = [x.to_json() for x in value]

                # Model, BaseModelMixin object (one-to-one relationship)
                elif issubclass(value_class, Model):
                    output[to_camelcase(attrName)] = value.to_json()

                # Datetime object
                elif isinstance(value, datetime):
                    output[to_camelcase(attrName)] = isoformat(value)

                elif isinstance(value, enum.Enum):
                    output[to_camelcase(attrName)] = value.name

                # Any primitive type
                else:
                    output[to_camelcase(attrName)] = value

        return output 
開發者ID:RiotGames,項目名稱:cloud-inquisitor,代碼行數:39,代碼來源:base.py

示例3: primary_key_names

# 需要導入模塊: from sqlalchemy.orm import attributes [as 別名]
# 或者: from sqlalchemy.orm.attributes import QueryableAttribute [as 別名]
def primary_key_names(model):
    """Returns all the primary keys for a model."""
    return [key for key, field in inspect.getmembers(model)
            if isinstance(field, QueryableAttribute)
            and isinstance(field.property, ColumnProperty)
            and field.property.columns[0].primary_key] 
開發者ID:yfauser,項目名稱:planespotter,代碼行數:8,代碼來源:helpers.py

示例4: apply_nested_fields

# 需要導入模塊: from sqlalchemy.orm import attributes [as 別名]
# 或者: from sqlalchemy.orm.attributes import QueryableAttribute [as 別名]
def apply_nested_fields(self, data, obj):
        nested_fields_to_apply = []
        nested_fields = get_nested_fields(self.resource.schema, model_field=True)
        for key, value in data.items():
            if key in nested_fields:
                nested_field_inspection = inspect(getattr(obj.__class__, key))

                if not isinstance(nested_field_inspection, QueryableAttribute):
                    raise InvalidType("Unrecognized nested field type: not a queryable attribute.")

                if isinstance(nested_field_inspection.property, RelationshipProperty):
                    nested_model = getattr(obj.__class__, key).property.mapper.class_

                    if isinstance(value, list):
                        nested_objects = []

                        for identifier in value:
                            nested_object = nested_model(**identifier)
                            nested_objects.append(nested_object)

                        nested_fields_to_apply.append({'field': key, 'value': nested_objects})
                    else:
                        nested_fields_to_apply.append({'field': key, 'value': nested_model(**value)})
                elif isinstance(nested_field_inspection.property, ColumnProperty):
                    nested_fields_to_apply.append({'field': key, 'value': value})
                else:
                    raise InvalidType("Unrecognized nested field type: not a RelationshipProperty or ColumnProperty.")

        for nested_field in nested_fields_to_apply:
            setattr(obj, nested_field['field'], nested_field['value']) 
開發者ID:miLibris,項目名稱:flask-rest-jsonapi,代碼行數:32,代碼來源:alchemy.py

示例5: derive_order_key

# 需要導入模塊: from sqlalchemy.orm import attributes [as 別名]
# 或者: from sqlalchemy.orm.attributes import QueryableAttribute [as 別名]
def derive_order_key(ocol, desc, index):
    """Attempt to derive the value of `ocol` from a query column.

    :param ocol: The :class:`OC` to look up.
    :param desc: Either a column description as in
        :attr:`sqlalchemy.orm.query.Query.column_descriptions`, or a
        :class:`sqlalchemy.sql.expression.ColumnElement`.

    :returns: Either a :class:`MappedOrderColumn` or `None`."""
    if isinstance(desc, ColumnElement):
        if desc.compare(ocol.comparable_value):
            return DirectColumn(ocol, index)
        else:
            return None

    entity = desc['entity']
    expr = desc['expr']

    if isinstance(expr, Bundle):
        for key, col in expr.columns.items():
            if strip_labels(col).compare(ocol.comparable_value):
                return AttributeColumn(ocol, index, key)

    try:
        is_a_table = bool(entity == expr)
    except (sqlalchemy.exc.ArgumentError, TypeError):
        is_a_table = False

    if isinstance(expr, Mapper) and expr.class_ == entity:
        is_a_table = True

    if is_a_table:  # is a table
        mapper = class_mapper(desc['type'])
        try:
            prop = mapper.get_property_by_column(ocol.element)
            return AttributeColumn(ocol, index, prop.key)
        except sqlalchemy.orm.exc.UnmappedColumnError:
            pass

    # is an attribute
    if isinstance(expr, QueryableAttribute):
        mapper = expr.parent
        tname = mapper.local_table.description
        if ocol.table_name == tname and ocol.name == expr.name:
            return DirectColumn(ocol, index)

    # is an attribute with label
    try:
        if ocol.quoted_full_name == OC(expr).full_name:
            return DirectColumn(ocol, index)
    except sqlalchemy.exc.ArgumentError:
        pass 
開發者ID:djrobstep,項目名稱:sqlakeyset,代碼行數:54,代碼來源:columns.py


注:本文中的sqlalchemy.orm.attributes.QueryableAttribute方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。