本文整理汇总了Python中sqlalchemy.ext.associationproxy.association_proxy方法的典型用法代码示例。如果您正苦于以下问题:Python associationproxy.association_proxy方法的具体用法?Python associationproxy.association_proxy怎么用?Python associationproxy.association_proxy使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.ext.associationproxy
的用法示例。
在下文中一共展示了associationproxy.association_proxy方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: annotation_association
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import association_proxy [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'))
)
示例2: if_data_association
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import association_proxy [as 别名]
def if_data_association(cls):
name = cls.__name__
discriminator = name.lower()
assoc_cls = type(
"%sIfDataAssociation" % name, (IfDataAssociation,),
dict(
__tablename__ = None,
__mapper_args__ = {"polymorphic_identity": discriminator},
),
)
cls.if_data = association_proxy(
"if_data_association",
"if_data",
creator = lambda if_data: assoc_cls(if_data = if_data),
)
return relationship(
assoc_cls, backref = backref("parent", uselist = False, collection_class = ordering_list('position'))
)
示例3: address_association
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import association_proxy [as 别名]
def address_association(cls):
name = cls.__name__
discriminator = name.lower()
assoc_cls = type(
"%sAddressAssociation" % name,
(AddressAssociation,),
dict(
__tablename__=None,
__mapper_args__={"polymorphic_identity": discriminator},
),
)
cls.addresses = association_proxy(
"address_association",
"addresses",
creator=lambda addresses: assoc_cls(addresses=addresses),
)
return relationship(
assoc_cls, backref=backref("parent", uselist=False)
)
示例4: test_lazy_list
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import association_proxy [as 别名]
def test_lazy_list(self):
Parent, Child = self.Parent, self.Child
mapper(
Parent,
self.table,
properties={
"_children": relationship(
Child, lazy="select", collection_class=list
)
},
)
p = Parent("p")
p.children = ["a", "b", "c"]
p = self.roundtrip(p)
# Is there a better way to ensure that the association_proxy
# didn't convert a lazy load to an eager load? This does work though.
self.assert_("_children" not in p.__dict__)
self.assert_(len(p._children) == 3)
self.assert_("_children" in p.__dict__)
示例5: setup
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import association_proxy [as 别名]
def setup(self):
metadata = MetaData(testing.db)
parents = Table(
"parents",
metadata,
Column(
"id", Integer, primary_key=True, test_needs_autoincrement=True
),
Column("name", String(30)),
)
children = Table(
"children",
metadata,
Column(
"id", Integer, primary_key=True, test_needs_autoincrement=True
),
Column("parent_id", Integer, ForeignKey("parents.id")),
Column("name", String(30)),
)
metadata.create_all()
parents.insert().execute(name="p1")
self.metadata = metadata
self.parents = parents
self.children = children
Parent.kids = association_proxy("children", "name")
示例6: test_resolve_aliased_class
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import association_proxy [as 别名]
def test_resolve_aliased_class(self):
Base = declarative_base()
class A(Base):
__tablename__ = "a"
id = Column(Integer, primary_key=True)
value = Column(String)
class B(Base):
__tablename__ = "b"
id = Column(Integer, primary_key=True)
a_id = Column(Integer, ForeignKey(A.id))
a = relationship(A)
a_value = association_proxy("a", "value")
spec = aliased(B).a_value
is_(spec.owning_class, B)
spec = B.a_value
is_(spec.owning_class, B)
示例7: test_resolved_to_correct_class_one
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import association_proxy [as 别名]
def test_resolved_to_correct_class_one(self):
Base = declarative_base()
class Parent(Base):
__tablename__ = "parent"
id = Column(Integer, primary_key=True)
_children = relationship("Child")
children = association_proxy("_children", "value")
class Child(Base):
__tablename__ = "child"
parent_id = Column(
Integer, ForeignKey(Parent.id), primary_key=True
)
value = Column(String)
class SubParent(Parent):
__tablename__ = "subparent"
id = Column(Integer, ForeignKey(Parent.id), primary_key=True)
is_(SubParent.children.owning_class, SubParent)
is_(Parent.children.owning_class, Parent)
示例8: test_resolved_to_correct_class_two
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import association_proxy [as 别名]
def test_resolved_to_correct_class_two(self):
Base = declarative_base()
class Parent(Base):
__tablename__ = "parent"
id = Column(Integer, primary_key=True)
_children = relationship("Child")
class Child(Base):
__tablename__ = "child"
parent_id = Column(
Integer, ForeignKey(Parent.id), primary_key=True
)
value = Column(String)
class SubParent(Parent):
__tablename__ = "subparent"
id = Column(Integer, ForeignKey(Parent.id), primary_key=True)
children = association_proxy("_children", "value")
is_(SubParent.children.owning_class, SubParent)
示例9: setup_classes
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import association_proxy [as 别名]
def setup_classes(cls):
from sqlalchemy.orm import synonym
Base = cls.DeclarativeBasic
class A(Base):
__tablename__ = "a"
id = Column(Integer, primary_key=True)
data = Column(String)
bs = relationship("B", backref="a")
data_syn = synonym("data")
b_data = association_proxy("bs", "data_syn")
class B(Base):
__tablename__ = "b"
id = Column(Integer, primary_key=True)
a_id = Column(ForeignKey("a.id"))
data = Column(String)
data_syn = synonym("data")
a_data = association_proxy("a", "data_syn")
示例10: create_class
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import association_proxy [as 别名]
def create_class(self, manager):
"""
Create TransactionMeta class.
"""
class TransactionMeta(
manager.declarative_base,
TransactionMetaBase
):
__tablename__ = 'transaction_meta'
TransactionMeta.transaction = sa.orm.relationship(
manager.transaction_cls,
backref=sa.orm.backref(
'meta_relation',
collection_class=attribute_mapped_collection('key')
),
primaryjoin=(
'%s.id == TransactionMeta.transaction_id' %
manager.transaction_cls.__name__
),
foreign_keys=[TransactionMeta.transaction_id]
)
manager.transaction_cls.meta = association_proxy(
'meta_relation',
'value',
creator=lambda key, value: TransactionMeta(key=key, value=value)
)
return TransactionMeta
示例11: tenant_name
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import association_proxy [as 别名]
def tenant_name(cls):
return association_proxy('tenant', 'name')
示例12: created_by
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import association_proxy [as 别名]
def created_by(cls):
return association_proxy('creator', 'username')
示例13: association_proxy
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import association_proxy [as 别名]
def association_proxy(*args, **kwargs):
if 'type' in kwargs:
type_ = kwargs.get('type')
del kwargs['type']
else:
type_ = ':obj:`basestring`'
proxy = original_association_proxy(*args, **kwargs)
proxy.__doc__ = """
Internal. For use in SQLAlchemy queries.
:type: {0}
""".format(type_)
return proxy
示例14: test_synonym_of_non_property_raises
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import association_proxy [as 别名]
def test_synonym_of_non_property_raises(self):
from sqlalchemy.ext.associationproxy import association_proxy
class User(object):
pass
users, Address, addresses = (
self.tables.users,
self.classes.Address,
self.tables.addresses,
)
mapper(
User,
users,
properties={"y": synonym("x"), "addresses": relationship(Address)},
)
mapper(Address, addresses)
User.x = association_proxy("addresses", "email_address")
assert_raises_message(
sa.exc.InvalidRequestError,
r'synonym\(\) attribute "User.x" only supports ORM mapped '
"attributes, got .*AssociationProxy",
getattr,
User.y,
"property",
)
示例15: _fixture
# 需要导入模块: from sqlalchemy.ext import associationproxy [as 别名]
# 或者: from sqlalchemy.ext.associationproxy import association_proxy [as 别名]
def _fixture(self, collection_class, is_dict=False):
class Parent(object):
collection = association_proxy("_collection", "child")
class Child(object):
def __init__(self, name):
self.name = name
class Association(object):
if is_dict:
def __init__(self, key, child):
self.child = child
else:
def __init__(self, child):
self.child = child
mapper(
Parent,
self.tables.parent,
properties={
"_collection": relationship(
Association,
collection_class=collection_class,
backref="parent",
)
},
)
mapper(
Association,
self.tables.association,
properties={"child": relationship(Child, backref="association")},
)
mapper(Child, self.tables.child)
return Parent, Child, Association