本文整理汇总了Python中sqlalchemy.orm.backref方法的典型用法代码示例。如果您正苦于以下问题:Python orm.backref方法的具体用法?Python orm.backref怎么用?Python orm.backref使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.orm
的用法示例。
在下文中一共展示了orm.backref方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: annotation_association
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import backref [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 import orm [as 别名]
# 或者: from sqlalchemy.orm import backref [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 import orm [as 别名]
# 或者: from sqlalchemy.orm import backref [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: _standard_bidirectional_fixture
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import backref [as 别名]
def _standard_bidirectional_fixture(self):
left, secondary, right = (
self.tables.left,
self.tables.secondary,
self.tables.right,
)
A, B = self.classes.A, self.classes.B
mapper(
A,
left,
properties={
"bs": relationship(
B, secondary=secondary, backref="as", order_by=right.c.id
)
},
)
mapper(B, right)
示例5: test_collection_move_preloaded
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import backref [as 别名]
def test_collection_move_preloaded(self):
User, Address = self.classes.User, self.classes.Address
sess = sessionmaker()()
a1 = Address(email_address="address1")
u1 = User(name="jack", addresses=[a1])
u2 = User(name="ed")
sess.add_all([u1, u2])
sess.commit() # everything is expired
# load u1.addresses collection
u1.addresses
u2.addresses.append(a1)
# backref fires
assert a1.user is u2
# a1 removed from u1.addresses as of [ticket:2789]
assert a1 not in u1.addresses
assert a1 in u2.addresses
示例6: test_collection_move_notloaded
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import backref [as 别名]
def test_collection_move_notloaded(self):
User, Address = self.classes.User, self.classes.Address
sess = sessionmaker()()
a1 = Address(email_address="address1")
u1 = User(name="jack", addresses=[a1])
u2 = User(name="ed")
sess.add_all([u1, u2])
sess.commit() # everything is expired
u2.addresses.append(a1)
# backref fires
assert a1.user is u2
# u1.addresses wasn't loaded,
# so when it loads its correct
assert a1 not in u1.addresses
assert a1 in u2.addresses
示例7: test_scalar_move_notloaded
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import backref [as 别名]
def test_scalar_move_notloaded(self):
User, Address = self.classes.User, self.classes.Address
sess = sessionmaker()()
u1 = User(name="jack")
u2 = User(name="ed")
a1 = Address(email_address="a1")
a1.user = u1
sess.add_all([u1, u2, a1])
sess.commit()
# direct set - the fetching of the
# "old" u1 here allows the backref
# to remove it from the addresses collection
a1.user = u2
assert a1 not in u1.addresses
assert a1 in u2.addresses
示例8: test_scalar_move_commitfirst
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import backref [as 别名]
def test_scalar_move_commitfirst(self):
User, Address = self.classes.User, self.classes.Address
sess = sessionmaker()()
u1 = User(name="jack")
u2 = User(name="ed")
a1 = Address(email_address="a1")
a1.user = u1
sess.add_all([u1, u2, a1])
sess.commit()
# u1.addresses is loaded
u1.addresses
# direct set - the fetching of the
# "old" u1 here allows the backref
# to remove it from the addresses collection
a1.user = u2
sess.commit()
assert a1 not in u1.addresses
assert a1 in u2.addresses
示例9: setup_mappers
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import backref [as 别名]
def setup_mappers(cls):
Address, addresses, users, User = (
cls.classes.Address,
cls.tables.addresses,
cls.tables.users,
cls.classes.User,
)
mapper(Address, addresses)
mapper(
User,
users,
properties={
"address": relationship(
Address, backref=backref("user"), uselist=False
)
},
)
示例10: test_scalar_move_preloaded
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import backref [as 别名]
def test_scalar_move_preloaded(self):
User, Address = self.classes.User, self.classes.Address
sess = sessionmaker()()
a1 = Address(email_address="address1")
a2 = Address(email_address="address1")
u1 = User(name="jack", address=a1)
sess.add_all([u1, a1, a2])
sess.commit() # everything is expired
# load a1.user
a1.user
# reassign
a2.user = u1
# backref fires
assert u1.address is a2
# stays on both sides
assert a1.user is u1
assert a2.user is u1
示例11: test_m2o_event
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import backref [as 别名]
def test_m2o_event(self):
User, Address = self.classes.User, self.classes.Address
sess = sessionmaker()()
a1 = Address(email_address="address1")
u1 = User(name="jack", address=a1)
sess.add(u1)
sess.commit()
sess.expunge(u1)
u2 = User(name="ed")
# the _SingleParent extension sets the backref get to "active" !
# u1 gets loaded and deleted
u2.address = a1
sess.commit()
assert sess.query(User).count() == 1
示例12: setup_mappers
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import backref [as 别名]
def setup_mappers(cls):
documents, Document, User, users = (
cls.tables.documents,
cls.classes.Document,
cls.classes.User,
cls.tables.users,
)
mapper(User, users)
mapper(
Document,
documents,
properties={
"user": relationship(
User,
lazy="joined",
backref=backref("documents", lazy="select"),
)
},
)
示例13: setup_mappers
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import backref [as 别名]
def setup_mappers(cls):
Address, addresses, users, User = (
cls.classes.Address,
cls.tables.addresses,
cls.tables.users,
cls.classes.User,
)
mapper(Address, addresses)
mapper(
User,
users,
properties={
"address": relationship(
Address,
backref=backref("user", single_parent=True),
uselist=False,
)
},
)
示例14: parent
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import backref [as 别名]
def parent(self):
return relationship(
self,
order_by=lambda: self.left,
foreign_keys=[self.parent_id],
remote_side="{}.{}".format(self.__name__, self.get_pk_name()),
backref=backref(
"children",
cascade="all,delete",
order_by=lambda: (self.tree_id, self.left),
),
)
示例15: home_team
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import backref [as 别名]
def home_team(cls):
return relationship('Clubs', foreign_keys="{}.home_team_id".format(cls.__name__),
backref=backref('home_friendly_matches'))