本文整理汇总了Python中sqlalchemy.ext.associationproxy.AssociationProxy方法的典型用法代码示例。如果您正苦于以下问题:Python associationproxy.AssociationProxy方法的具体用法?Python associationproxy.AssociationProxy怎么用?Python associationproxy.AssociationProxy使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.ext.associationproxy
的用法示例。
在下文中一共展示了associationproxy.AssociationProxy方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_field_type
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import AssociationProxy [as 别名]
def get_field_type(model, fieldname):
"""Helper which returns the SQLAlchemy type of the field.
"""
field = getattr(model, fieldname)
if isinstance(field, ColumnElement):
fieldtype = field.type
else:
if isinstance(field, AssociationProxy):
field = field.remote_attr
if hasattr(field, 'property'):
prop = field.property
if isinstance(prop, RelProperty):
return None
fieldtype = prop.columns[0].type
else:
return None
return fieldtype
示例2: is_like_list
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import AssociationProxy [as 别名]
def is_like_list(instance, relation):
"""Returns ``True`` if and only if the relation of `instance` whose name is
`relation` is list-like.
A relation may be like a list if, for example, it is a non-lazy one-to-many
relation, or it is a dynamically loaded one-to-many.
"""
if relation in instance._sa_class_manager:
return instance._sa_class_manager[relation].property.uselist
elif hasattr(instance, relation):
attr = getattr(instance._sa_instance_state.class_, relation)
if hasattr(attr, 'property'):
return attr.property.uselist
related_value = getattr(type(instance), relation, None)
if isinstance(related_value, AssociationProxy):
local_prop = related_value.local_attr.prop
if isinstance(local_prop, RelProperty):
return local_prop.uselist
return False
示例3: maybe_convert_values
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import AssociationProxy [as 别名]
def maybe_convert_values(self, model_class, data):
ret = data.copy()
for col_name, value in data.items():
col = getattr(model_class, col_name)
if isinstance(col, AssociationProxy) or col.impl.uses_objects:
ret[col_name] = self.convert_identifiers(value)
elif not hasattr(col, 'type'):
continue
elif isinstance(col.type, Date):
if value in ('today', 'now', 'utcnow'):
ret[col_name] = utcnow().date
else:
ret[col_name] = parse_datetime(value).date
elif isinstance(col.type, DateTime):
if value in ('now', 'utcnow'):
ret[col_name] = utcnow()
elif not isinstance(value, datetime.datetime):
ret[col_name] = parse_datetime(value)
return ret
示例4: is_column_array
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import AssociationProxy [as 别名]
def is_column_array(self, name: str) -> bool:
# Well, even though this column is clearly an array, it does not behave like one when thought of in terms of
# Postgres operators, because the underlying comparison is done to a scalar column.
# Example: AssociationProxy to User.name will use the `name` column for comparisons, which is scalar!
return False
示例5: _get_model_association_proxies
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import AssociationProxy [as 别名]
def _get_model_association_proxies(model, ins):
""" Get a dict of model association_proxy attributes """
# Ignore AssociationProxy attrs for SA 1.2.x
if SA_12:
warnings.warn('MongoSQL only supports AssociationProxy columns with SqlAlchemy 1.3.x')
return {}
return {name: getattr(model, name)
for name, c in ins.all_orm_descriptors.items()
if not name.startswith('_')
and isinstance(c, AssociationProxy)}
示例6: related_query
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import AssociationProxy [as 别名]
def related_query(self, obj_id, relationship, full_object=True):
"""Construct query for related objects.
Parameters:
obj_id (str): id of an item in this view's collection.
relationship: the relationship object to get related objects from.
This can be a RelationshipProperty or AssociationProxy object.
related_to (model class or None): the class the relationship is
coming from. AssociationProxy relationships use this. It
defaults to ``None``, which is interpreted as self.model.
full_object (bool): if full_object is ``True``, query for all
requested columns (probably to build resource objects). If
full_object is False, only query for the key column (probably
to build resource identifiers).
Returns:
sqlalchemy.orm.query.Query: query which will fetch related
object(s).
"""
if isinstance(relationship.obj, AssociationProxy):
query = self.association_proxy_query(
obj_id, relationship, full_object=full_object
)
else:
query = self.standard_relationship_query(
obj_id, relationship, full_object=full_object
)
return query
示例7: get_related_model
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import AssociationProxy [as 别名]
def get_related_model(model, relationname):
"""Gets the class of the model to which `model` is related by the attribute
whose name is `relationname`.
"""
if hasattr(model, relationname):
attr = getattr(model, relationname)
if hasattr(attr, 'property') \
and isinstance(attr.property, RelProperty):
return attr.property.mapper.class_
if isinstance(attr, AssociationProxy):
return get_related_association_proxy_model(attr)
return None
示例8: _iter_association_proxies
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import AssociationProxy [as 别名]
def _iter_association_proxies(cls):
for col, value in vars(cls).items():
if isinstance(value, associationproxy.AssociationProxy):
yield col
示例9: _sub_operator
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import AssociationProxy [as 别名]
def _sub_operator(model, argument, fieldname):
"""Recursively calls :func:`QueryBuilder._create_operation` when argument
is a dictionary of the form specified in :ref:`search`.
This function is for use with the ``has`` and ``any`` search operations.
"""
if isinstance(model, InstrumentedAttribute):
submodel = model.property.mapper.class_
elif isinstance(model, AssociationProxy):
submodel = get_related_association_proxy_model(model)
else: # TODO what to do here?
pass
if isinstance(argument, dict):
fieldname = argument['name']
operator = argument['op']
argument = argument.get('val')
relation = None
if '__' in fieldname:
fieldname, relation = fieldname.split('__')
return QueryBuilder._create_operation(submodel, fieldname, operator,
argument, relation)
# Support legacy has/any with implicit eq operator
return getattr(submodel, fieldname) == argument
#: The mapping from operator name (as accepted by the search method) to a
#: function which returns the SQLAlchemy expression corresponding to that
#: operator.
#:
#: Each of these functions accepts either one, two, or three arguments. The
#: first argument is the field object on which to apply the operator. The
#: second argument, where it exists, is either the second argument to the
#: operator or a dictionary as described below. The third argument, where it
#: exists, is the name of the field.
#:
#: For functions that accept three arguments, the second argument may be a
#: dictionary containing ``'name'``, ``'op'``, and ``'val'`` mappings so that
#: :func:`QueryBuilder._create_operation` may be applied recursively. For more
#: information and examples, see :ref:`search`.
#:
#: Some operations have multiple names. For example, the equality operation can
#: be described by the strings ``'=='``, ``'eq'``, ``'equals'``, etc.