本文整理汇总了Python中sqlalchemy.orm.RelationshipProperty方法的典型用法代码示例。如果您正苦于以下问题:Python orm.RelationshipProperty方法的具体用法?Python orm.RelationshipProperty怎么用?Python orm.RelationshipProperty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.orm
的用法示例。
在下文中一共展示了orm.RelationshipProperty方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: object_as_dict
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import RelationshipProperty [as 别名]
def object_as_dict(obj, relationships=False):
"""
Converts an SQLAlchemy instance to a dictionary.
:param relationships: If true, also include relationships in the output dict
"""
properties = inspect(obj).mapper.all_orm_descriptors
if not relationships:
properties = {
key: value
for key, value in properties.items()
if not hasattr(value, "prop")
or not isinstance(value.prop, RelationshipProperty)
}
return {key: getattr(obj, key) for key, value in properties.items()}
示例2: contribute_to_class
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import RelationshipProperty [as 别名]
def contribute_to_class(self, mcs_args: McsArgs, relationships):
if mcs_args.Meta.abstract:
return
discovered_relationships = {}
def discover_relationships(d):
for k, v in d.items():
if isinstance(v, RelationshipProperty):
discovered_relationships[v.argument] = k
if v.backref and mcs_args.Meta.lazy_mapped:
raise NotImplementedError(
f'Discovered a lazy-mapped backref `{k}` on '
f'`{mcs_args.qualname}`. Currently this '
'is unsupported; please use `db.relationship` with '
'the `back_populates` kwarg on both sides instead.')
for base in mcs_args.bases:
discover_relationships(vars(base))
discover_relationships(mcs_args.clsdict)
relationships.update(discovered_relationships)
示例3: get_field_type
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import RelationshipProperty [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
示例4: is_like_list
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import RelationshipProperty [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
示例5: __init__
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import RelationshipProperty [as 别名]
def __init__(self, relationships: Mapping[str, RelationshipProperty]):
""" Init relationships
:param relationships: Model relationships
"""
super(RelationshipsBag, self).__init__()
self._relations = relationships
self._rel_names = frozenset(self._relations.keys())
self._array_rel_names = frozenset(name
for name, rel in self._relations.items()
if _is_relationship_array(rel))
示例6: __iter__
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import RelationshipProperty [as 别名]
def __iter__(self) -> Iterable[Tuple[str, RelationshipProperty]]:
""" Get relationships """
return iter(self._relations.items())
示例7: __getitem__
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import RelationshipProperty [as 别名]
def __getitem__(self, name: str) -> RelationshipProperty:
return self._relations[name]
示例8: get_relationship
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import RelationshipProperty [as 别名]
def get_relationship(self, col_name: str) -> RelationshipProperty:
return self._rel_bag[self.get_relationship_name(col_name)]
示例9: _is_relationship_array
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import RelationshipProperty [as 别名]
def _is_relationship_array(rel: RelationshipProperty) -> bool:
""" Is the relationship an array relationship? """
return rel.property.uselist
示例10: as_relation_of
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import RelationshipProperty [as 别名]
def as_relation_of(self, mongoquery: 'MongoQuery', relationship: RelationshipProperty) -> 'MongoQuery':
""" Handle the query as a sub-query handling a relationship
This is used by the MongoJoin handler to build queries to related models.
:param mongoquery: The parent query
:param relationship: The relationship
"""
return self.as_relation(mongoquery._join_path + (relationship,))
示例11: relations
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import RelationshipProperty [as 别名]
def relations(cls):
"""Return a `list` of relationship names or the given model
"""
return [c.key for c in cls.__mapper__.iterate_properties
if isinstance(c, RelationshipProperty)]
示例12: test_columns
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import RelationshipProperty [as 别名]
def test_columns(self, table):
"""Tests the columns in database table. If a column exists in the
table, check that the column has correct datatype. If the column
doesn't exist, add a MissingColumn object to the error list.
"""
columns = self.inspection_engine.get_columns(table)
column_names = [c["name"] for c in columns]
# Search for correct class from models for the mapper.
mapper = None
for name, class_ in self.model_tables:
if class_.__tablename__ == table:
mapper = inspect(class_)
break
if not mapper:
return
for column_property in mapper.attrs:
if isinstance(column_property, RelationshipProperty):
# TODO: Add RelationshipProperty sanity checking.
pass
else:
for column in column_property.columns:
if column.key in column_names:
self.test_datatype(table, column)
self.test_nullable(table, column)
else:
self.errors.append(MissingColumn(table, column.key,
parent=self))
示例13: _should_exclude_field
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import RelationshipProperty [as 别名]
def _should_exclude_field(column, fields=None, exclude=None,
include_related=None):
if fields and column.key not in fields:
return True
if exclude and column.key in exclude:
return True
if isinstance(column, RelationshipProperty) and not include_related:
return True
return False
示例14: get_related_model
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import RelationshipProperty [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
示例15: apply_nested_fields
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import RelationshipProperty [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'])