本文整理汇总了Python中sqlalchemy.orm.relationship方法的典型用法代码示例。如果您正苦于以下问题:Python orm.relationship方法的具体用法?Python orm.relationship怎么用?Python orm.relationship使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.orm
的用法示例。
在下文中一共展示了orm.relationship方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _memoized_attr_info
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import relationship [as 别名]
def _memoized_attr_info(self):
"""Info dictionary associated with the object, allowing user-defined
data to be associated with this :class:`.InspectionAttr`.
The dictionary is generated when first accessed. Alternatively,
it can be specified as a constructor argument to the
:func:`.column_property`, :func:`.relationship`, or :func:`.composite`
functions.
.. versionadded:: 0.8 Added support for .info to all
:class:`.MapperProperty` subclasses.
.. versionchanged:: 1.0.0 :attr:`.MapperProperty.info` is also
available on extension types via the
:attr:`.InspectionAttrInfo.info` attribute, so that it can apply
to a wider variety of ORM and extension constructs.
.. seealso::
:attr:`.QueryableAttribute.info`
:attr:`.SchemaItem.info`
"""
return {}
示例2: annotation_association
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import relationship [as 别名]
def annotation_association(cls):
name = cls.__name__
discriminator = name.lower()
assoc_cls = type(
"%sAnnotationAssociation" % name, (AnnotationAssociation,),
dict(
__tablename__ = None,
__mapper_args__ = {"polymorphic_identity": discriminator},
),
)
cls.annotation = association_proxy(
"annotation_association",
"annotation",
creator = lambda annotation: assoc_cls(annotation = annotation),
)
return relationship(
assoc_cls, backref = backref("parent", uselist = False, collection_class = ordering_list('position'))
)
示例3: ReferenceCol
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import relationship [as 别名]
def ReferenceCol(tablename, nullable=False, pk_name='id', **kwargs):
"""Column that adds primary key foreign key reference.
Usage: ::
category_id = ReferenceCol('category')
category = relationship('Category', backref='categories')
"""
return db.Column(
db.ForeignKey("{0}.{1}".format(tablename, pk_name)),
nullable=nullable, **kwargs)
示例4: __init__
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import relationship [as 别名]
def __init__(self, private_key=None, wei_target_balance=None, wei_topup_threshold=None):
if private_key:
self.private_key = private_key
else:
self.private_key = Web3.toHex(keccak(os.urandom(4096)))
self.wei_target_balance = wei_target_balance
self.wei_topup_threshold = wei_topup_threshold
# https://stackoverflow.com/questions/20830118/creating-a-self-referencing-m2m-relationship-in-sqlalchemy-flask
示例5: to_json
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import relationship [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
示例6: cuser
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import relationship [as 别名]
def cuser(cls):
return relationship('User', remote_side=User.id,
foreign_keys='{}.cuid'.format(cls.__name__), doc="Created by")
示例7: roles
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import relationship [as 别名]
def roles(cls):
return FsModels.db.relationship(
"Role",
secondary=FsModels.roles_users,
backref=FsModels.db.backref("users", lazy="dynamic"),
)
示例8: user
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import relationship [as 别名]
def user(cls):
return relationship("User")
示例9: client_id
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import relationship [as 别名]
def client_id(cls):
return Column(
Integer, ForeignKey("oauth2_client.id", ondelete="CASCADE"), nullable=False
)
# client = relationship("fs_oauth2_client")
示例10: transformations
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import relationship [as 别名]
def transformations(self, relationship="all"):
"""Get all the transformations of this info.
Return a list of transformations involving this info. ``relationship``
can be "parent" (in which case only transformations where the info is
the ``info_in`` are returned), "child" (in which case only
transformations where the info is the ``info_out`` are returned) or
``all`` (in which case any transformations where the info is the
``info_out`` or the ``info_in`` are returned). The default is ``all``
"""
if relationship not in ["all", "parent", "child"]:
raise ValueError(
"You cannot get transformations of relationship {}".format(relationship)
+ "Relationship can only be parent, child or all."
)
if relationship == "all":
return Transformation.query.filter(
and_(
Transformation.failed == false(),
or_(
Transformation.info_in == self, Transformation.info_out == self
),
)
).all()
if relationship == "parent":
return Transformation.query.filter_by(
info_in_id=self.id, failed=False
).all()
if relationship == "child":
return Transformation.query.filter_by(
info_out_id=self.id, failed=False
).all()
示例11: actual_amount
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import relationship [as 别名]
def actual_amount(cls):
# see: http://docs.sqlalchemy.org/en/latest/orm/extensions/hybrid.html
# #correlated-subquery-relationship-hybrid
return select(
[func.sum(BudgetTransaction.amount)]
).where(
BudgetTransaction.trans_id.__eq__(cls.id)
).label('actual_amount')
示例12: reference_col
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import relationship [as 别名]
def reference_col(tablename, nullable=False, pk_name='id', **kwargs):
"""Column that adds primary key foreign key reference.
Usage: ::
category_id = reference_col('category')
category = relationship('Category', backref='categories')
"""
return db.Column(
db.ForeignKey('{0}.{1}'.format(tablename, pk_name)),
nullable=nullable, **kwargs)
示例13: test_onetomany
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import relationship [as 别名]
def test_onetomany(metadata):
Table(
"simple_items",
metadata,
Column("id", INTEGER, primary_key=True),
Column("container_id", INTEGER),
ForeignKeyConstraint(["container_id"], ["simple_containers.id"]),
)
Table("simple_containers", metadata, Column("id", INTEGER, primary_key=True))
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, ForeignKey, Integer
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class SimpleContainer(Base):
__tablename__ = 'simple_containers'
id = Column(Integer, primary_key=True)
class SimpleItem(Base):
__tablename__ = 'simple_items'
id = Column(Integer, primary_key=True)
container_id = Column(ForeignKey('simple_containers.id'))
container = relationship('SimpleContainer')
"""
)
示例14: test_onetomany_selfref
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import relationship [as 别名]
def test_onetomany_selfref(metadata):
Table(
"simple_items",
metadata,
Column("id", INTEGER, primary_key=True),
Column("parent_item_id", INTEGER),
ForeignKeyConstraint(["parent_item_id"], ["simple_items.id"]),
)
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, ForeignKey, Integer
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class SimpleItem(Base):
__tablename__ = 'simple_items'
id = Column(Integer, primary_key=True)
parent_item_id = Column(ForeignKey('simple_items.id'))
parent_item = relationship('SimpleItem', remote_side=[id])
"""
)
示例15: test_onetomany_selfref_multi
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import relationship [as 别名]
def test_onetomany_selfref_multi(metadata):
Table(
"simple_items",
metadata,
Column("id", INTEGER, primary_key=True),
Column("parent_item_id", INTEGER),
Column("top_item_id", INTEGER),
ForeignKeyConstraint(["parent_item_id"], ["simple_items.id"]),
ForeignKeyConstraint(["top_item_id"], ["simple_items.id"]),
)
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, ForeignKey, Integer
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class SimpleItem(Base):
__tablename__ = 'simple_items'
id = Column(Integer, primary_key=True)
parent_item_id = Column(ForeignKey('simple_items.id'))
top_item_id = Column(ForeignKey('simple_items.id'))
parent_item = relationship('SimpleItem', remote_side=[id], \
primaryjoin='SimpleItem.parent_item_id == SimpleItem.id')
top_item = relationship('SimpleItem', remote_side=[id], \
primaryjoin='SimpleItem.top_item_id == SimpleItem.id')
"""
)