本文整理汇总了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.'
)
示例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
示例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]
示例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'])
示例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