本文整理汇总了Python中sqlalchemy.orm.collections.attribute_mapped_collection方法的典型用法代码示例。如果您正苦于以下问题:Python collections.attribute_mapped_collection方法的具体用法?Python collections.attribute_mapped_collection怎么用?Python collections.attribute_mapped_collection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.orm.collections
的用法示例。
在下文中一共展示了collections.attribute_mapped_collection方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_attribute_mapped_collection
# 需要导入模块: from sqlalchemy.orm import collections [as 别名]
# 或者: from sqlalchemy.orm.collections import attribute_mapped_collection [as 别名]
def test_attribute_mapped_collection(self):
users, addresses = self.tables.users, self.tables.addresses
mapper(
User,
users,
properties={
"addresses": relationship(
Address,
collection_class=attribute_mapped_collection(
"email_address"
),
)
},
)
mapper(Address, addresses)
u1 = User()
u1.addresses = {"email1": Address(email_address="email1")}
for loads, dumps in picklers():
repickled = loads(dumps(u1))
eq_(u1.addresses, repickled.addresses)
eq_(repickled.addresses["email1"], Address(email_address="email1"))
示例2: create_class
# 需要导入模块: from sqlalchemy.orm import collections [as 别名]
# 或者: from sqlalchemy.orm.collections import attribute_mapped_collection [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
示例3: test_attr_mapped_collection
# 需要导入模块: from sqlalchemy.orm import collections [as 别名]
# 或者: from sqlalchemy.orm.collections import attribute_mapped_collection [as 别名]
def test_attr_mapped_collection(self):
collection_class = collections.attribute_mapped_collection("a")
self._test_scalar_mapped(collection_class)
示例4: test_merge_irregular_collection
# 需要导入模块: from sqlalchemy.orm import collections [as 别名]
# 或者: from sqlalchemy.orm.collections import attribute_mapped_collection [as 别名]
def test_merge_irregular_collection(self):
users, Address, addresses, User = (
self.tables.users,
self.classes.Address,
self.tables.addresses,
self.classes.User,
)
mapper(
User,
users,
properties={
"addresses": relationship(
mapper(Address, addresses),
backref="user",
collection_class=attribute_mapped_collection(
"email_address"
),
)
},
)
u1 = User(id=7, name="fred")
u1.addresses["foo@bar.com"] = Address(email_address="foo@bar.com")
sess = create_session()
sess.merge(u1)
sess.flush()
assert list(u1.addresses.keys()) == ["foo@bar.com"]
示例5: setup
# 需要导入模块: from sqlalchemy.orm import collections [as 别名]
# 或者: from sqlalchemy.orm.collections import attribute_mapped_collection [as 别名]
def setup(self):
class B(object):
def __init__(self, key, elem):
self.key = key
self.elem = elem
class A(object):
elements = association_proxy("orig", "elem", creator=B)
m = MetaData()
a = Table("a", m, Column("id", Integer, primary_key=True))
b = Table(
"b",
m,
Column("id", Integer, primary_key=True),
Column("aid", Integer, ForeignKey("a.id")),
Column("elem", String),
)
mapper(
A,
a,
properties={
"orig": relationship(
B, collection_class=attribute_mapped_collection("key")
)
},
)
mapper(B, b)
self.A = A
self.B = B
示例6: test_dict_collections
# 需要导入模块: from sqlalchemy.orm import collections [as 别名]
# 或者: from sqlalchemy.orm.collections import attribute_mapped_collection [as 别名]
def test_dict_collections(self):
# TODO: break into individual tests
class Foo(fixtures.BasicEntity):
pass
class Bar(fixtures.BasicEntity):
pass
from sqlalchemy.orm.collections import attribute_mapped_collection
instrumentation.register_class(Foo)
instrumentation.register_class(Bar)
attributes.register_attribute(
Foo,
"someattr",
uselist=True,
useobject=True,
typecallable=attribute_mapped_collection("name"),
)
hi = Bar(name="hi")
there = Bar(name="there")
f = Foo()
eq_(
attributes.get_state_history(
attributes.instance_state(f), "someattr"
),
((), [], ()),
)
f.someattr["hi"] = hi
eq_(
attributes.get_state_history(
attributes.instance_state(f), "someattr"
),
([hi], [], []),
)
f.someattr["there"] = there
eq_(
tuple(
[
set(x)
for x in attributes.get_state_history(
attributes.instance_state(f), "someattr"
)
]
),
(set([hi, there]), set(), set()),
)
self._commit_someattr(f)
eq_(
tuple(
[
set(x)
for x in attributes.get_state_history(
attributes.instance_state(f), "someattr"
)
]
),
(set(), set([hi, there]), set()),
)
示例7: test_validator_bulk_dict_set
# 需要导入模块: from sqlalchemy.orm import collections [as 别名]
# 或者: from sqlalchemy.orm.collections import attribute_mapped_collection [as 别名]
def test_validator_bulk_dict_set(self):
users, addresses, Address = (
self.tables.users,
self.tables.addresses,
self.classes.Address,
)
class User(fixtures.ComparableEntity):
@validates("addresses", include_removes=True)
def validate_address(self, key, item, remove):
if not remove:
assert isinstance(item, str)
else:
assert isinstance(item, Address)
item = Address(email_address=item)
return item
mapper(
User,
users,
properties={
"addresses": relationship(
Address,
collection_class=collections.attribute_mapped_collection(
"email_address"
),
)
},
)
mapper(Address, addresses)
u1 = User()
u1.addresses["e1"] = "e1"
u1.addresses["e2"] = "e2"
eq_(
u1.addresses,
{
"e1": Address(email_address="e1"),
"e2": Address(email_address="e2"),
},
)
u1.addresses = {"e3": "e3", "e4": "e4"}
eq_(
u1.addresses,
{
"e3": Address(email_address="e3"),
"e4": Address(email_address="e4"),
},
)
示例8: setup
# 需要导入模块: from sqlalchemy.orm import collections [as 别名]
# 或者: from sqlalchemy.orm.collections import attribute_mapped_collection [as 别名]
def setup():
"""
Set up ORM.
Does not create any database tables, see :py:func:`create_tables`
for that.
"""
global resource_metadata_table
if resource_metadata_table is None:
log.debug('Defining resource metadata table')
resource_metadata_table = Table(
RESOURCE_METADATA_TABLE_NAME,
metadata,
Column('resource_id', types.UnicodeText, ForeignKey('resource.id',
ondelete='CASCADE', onupdate='CASCADE'), nullable=False,
primary_key=True),
Column('last_extracted', types.DateTime),
Column('last_url', types.UnicodeText),
Column('last_format', types.UnicodeText),
Column('task_id', types.UnicodeText)
)
mapper(
ResourceMetadata,
resource_metadata_table,
properties={
'_meta': relationship(ResourceMetadatum, collection_class=
attribute_mapped_collection('key'),
cascade='all, delete, delete-orphan'),
}
)
else:
log.debug('Resource metadata table already defined')
global resource_metadatum_table
if resource_metadatum_table is None:
log.debug('Defining resource metadatum table')
resource_metadatum_table = Table(
RESOURCE_METADATUM_TABLE_NAME,
metadata,
Column('id', types.Integer, nullable=False, primary_key=True),
Column('resource_id', types.UnicodeText, ForeignKey(
RESOURCE_METADATA_TABLE_NAME + '.resource_id',
ondelete='CASCADE', onupdate='CASCADE'), nullable=False),
Column('key', types.UnicodeText, nullable=False),
Column('value', types.UnicodeText)
)
mapper(ResourceMetadatum, resource_metadatum_table)
else:
log.debug('Resource metadatum table already defined')