本文整理汇总了Python中sqlalchemy.orm.lazyload方法的典型用法代码示例。如果您正苦于以下问题:Python orm.lazyload方法的具体用法?Python orm.lazyload怎么用?Python orm.lazyload使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.orm
的用法示例。
在下文中一共展示了orm.lazyload方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_refresh_with_lazy
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import lazyload [as 别名]
def test_refresh_with_lazy(self):
"""test that when a lazy loader is set as a trigger on an object's
attribute (at the attribute level, not the class level), a refresh()
operation doesn't fire the lazy loader or create any problems"""
User, Address, addresses, users = (
self.classes.User,
self.classes.Address,
self.tables.addresses,
self.tables.users,
)
s = create_session()
mapper(
User,
users,
properties={"addresses": relationship(mapper(Address, addresses))},
)
q = s.query(User).options(sa.orm.lazyload("addresses"))
u = q.filter(users.c.id == 8).first()
def go():
s.refresh(u)
self.assert_sql_count(testing.db, go, 1)
示例2: test_instance_lazy_relation_loaders
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import lazyload [as 别名]
def test_instance_lazy_relation_loaders(self):
users, addresses = (self.tables.users, self.tables.addresses)
mapper(
User,
users,
properties={"addresses": relationship(Address, lazy="noload")},
)
mapper(Address, addresses)
sess = Session()
u1 = User(name="ed", addresses=[Address(email_address="ed@bar.com")])
sess.add(u1)
sess.commit()
sess.close()
u1 = sess.query(User).options(lazyload(User.addresses)).first()
u2 = pickle.loads(pickle.dumps(u1))
sess = Session()
sess.add(u2)
assert u2.addresses
示例3: test_m2o_joinedload_not_others
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import lazyload [as 别名]
def test_m2o_joinedload_not_others(self):
self._eagerload_mappings(addresses_lazy="joined")
Address = self.classes.Address
sess = create_session()
q = (
sess.query(Address)
.options(lazyload("*"), joinedload("user"))
.yield_per(1)
.filter_by(id=1)
)
def go():
result = q.all()
assert result[0].user
self.assert_sql_count(testing.db, go, 1)
示例4: test_safe_unbound_option_allows_bake
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import lazyload [as 别名]
def test_safe_unbound_option_allows_bake(self):
User, Address, Dingaling = self._o2m_twolevel_fixture(lazy="joined")
lru = Address.dingalings.property._lazy_strategy._bakery(
lambda q: None
)._bakery
l1 = len(lru)
for i in range(5):
sess = Session()
u1 = (
sess.query(User)
.options(
defaultload(User.addresses).lazyload(Address.dingalings)
)
.first()
)
for ad in u1.addresses:
ad.dingalings
l2 = len(lru)
eq_(l1, 0)
eq_(l2, 2)
示例5: test_safe_bound_option_allows_bake
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import lazyload [as 别名]
def test_safe_bound_option_allows_bake(self):
User, Address, Dingaling = self._o2m_twolevel_fixture(lazy="joined")
lru = Address.dingalings.property._lazy_strategy._bakery(
lambda q: None
)._bakery
l1 = len(lru)
for i in range(5):
sess = Session()
u1 = (
sess.query(User)
.options(
Load(User)
.defaultload(User.addresses)
.lazyload(Address.dingalings)
)
.first()
)
for ad in u1.addresses:
ad.dingalings
l2 = len(lru)
eq_(l1, 0)
eq_(l2, 2)
示例6: test_model_history__both_classes
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import lazyload [as 别名]
def test_model_history__both_classes(self):
ssn = self.Session()
# Get a user from the DB
user = ssn.query(models.User).options(
lazyload('*')
).get(1)
# Prepare two history objects
old_user = ModelHistoryProxy(user)
# Check `user` properties
self.assertEqual(user.id, 1)
self.assertEqual(user.name, 'a')
self.assertEqual(user.age, 18)
self.assertEqual(user.tags, ['1', 'a'])
# === Test: columns
# Check `old_user` properties
# self.assertEqual(old_user.id, 1)
self.assertEqual(old_user.name, 'a')
self.assertEqual(old_user.age, 18)
self.assertEqual(old_user.tags, ['1', 'a'])
# Change `user`
user.id = 1000
user.name = 'aaaa'
user.age = 1800
user.tags = [1000,]
# Check `old_user` retains properties
self.assertEqual(old_user.id, 1)
self.assertEqual(old_user.name, 'a')
self.assertEqual(old_user.age, 18)
self.assertEqual(old_user.tags, ['1', 'a'])
# Undo
ssn.close()
# Older tests
示例7: get_count
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import lazyload [as 别名]
def get_count(q):
"""
Count the number of rows in a table. More efficient than count(*)
:param q:
:return:
"""
disable_group_by = False
if len(q._entities) > 1:
# currently support only one entity
raise Exception("only one entity is supported for get_count, got: %s" % q)
entity = q._entities[0]
if hasattr(entity, "column"):
# _ColumnEntity has column attr - on case: query(Model.column)...
col = entity.column
if q._group_by and q._distinct:
# which query can have both?
raise NotImplementedError
if q._group_by or q._distinct:
col = distinct(col)
if q._group_by:
# need to disable group_by and enable distinct - we can do this because we have only 1 entity
disable_group_by = True
count_func = func.count(col)
else:
# _MapperEntity doesn't have column attr - on case: query(Model)...
count_func = func.count()
if q._group_by and not disable_group_by:
count_func = count_func.over(None)
count_q = (
q.options(lazyload("*"))
.statement.with_only_columns([count_func])
.order_by(None)
)
if disable_group_by:
count_q = count_q.group_by(None)
count = q.session.execute(count_q).scalar()
return count
示例8: make_query
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import lazyload [as 别名]
def make_query(self, model):
cols = ['storage_backend', 'storage_file_id', 'md5']
if model.add_file_date_column:
cols.append('created_dt')
opts = QUERY_OPTIONS.get(model)
return (model.query
.filter(model.storage_file_id.isnot(None), model.storage_backend.in_(self.source_backend_names))
.filter_by(**SPECIAL_FILTERS.get(model, {}))
.options(*((opts,) if opts else ()))
.options(lazyload('*'), load_only(*cols)))
示例9: find_document
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import lazyload [as 别名]
def find_document(path, compareto):
query = session.query(Document)
attribute = Document._root
for i, match in enumerate(
re.finditer(r"/([\w_]+)(?:\[@([\w_]+)(?:=(.*))?\])?", path)
):
(token, attrname, attrvalue) = match.group(1, 2, 3)
target_node = aliased(_Node)
query = query.join(attribute.of_type(target_node)).filter(
target_node.tag == token
)
attribute = target_node.children
if attrname:
attribute_entity = aliased(_Attribute)
if attrvalue:
query = query.join(
target_node.attributes.of_type(attribute_entity)
).filter(
and_(
attribute_entity.name == attrname,
attribute_entity.value == attrvalue,
)
)
else:
query = query.join(
target_node.attributes.of_type(attribute_entity)
).filter(attribute_entity.name == attrname)
return (
query.options(lazyload(Document._root))
.filter(target_node.text == compareto)
.all()
)
示例10: find_document
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import lazyload [as 别名]
def find_document(path, compareto):
query = session.query(Document)
for i, match in enumerate(
re.finditer(r"/([\w_]+)(?:\[@([\w_]+)(?:=(.*))?\])?", path)
):
(token, attrname, attrvalue) = match.group(1, 2, 3)
if not i:
parent = Document
target_node = aliased(_Node)
query = query.join(parent._nodes.of_type(target_node)).filter(
target_node.parent_id.is_(None)
)
else:
parent = target_node
target_node = aliased(_Node)
query = query.join(parent.children.of_type(target_node))
query = query.filter(target_node.tag == token)
if attrname:
attribute_entity = aliased(_Attribute)
query = query.join(
target_node.attributes.of_type(attribute_entity)
)
if attrvalue:
query = query.filter(
and_(
attribute_entity.name == attrname,
attribute_entity.value == attrvalue,
)
)
else:
query = query.filter(attribute_entity.name == attrname)
return (
query.options(lazyload(Document._nodes))
.filter(target_node.text == compareto)
.all()
)
示例11: test_useget_cancels_eager
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import lazyload [as 别名]
def test_useget_cancels_eager(self):
"""test that a one to many lazyload cancels the unnecessary
eager many-to-one join on the other side."""
users, Address, addresses, User = (
self.tables.users,
self.classes.Address,
self.tables.addresses,
self.classes.User,
)
mapper(User, users)
mapper(
Address,
addresses,
properties={
"user": relationship(User, lazy="joined", backref="addresses")
},
)
sess = create_session()
u1 = sess.query(User).filter(User.id == 8).one()
def go():
eq_(u1.addresses[0].user, u1)
self.assert_sql_execution(
testing.db,
go,
CompiledSQL(
"SELECT addresses.id AS addresses_id, addresses.user_id AS "
"addresses_user_id, addresses.email_address AS "
"addresses_email_address FROM addresses WHERE :param_1 = "
"addresses.user_id",
{"param_1": 8},
),
)
示例12: test_aliased_unbound_are_now_safe_to_cache
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import lazyload [as 别名]
def test_aliased_unbound_are_now_safe_to_cache(self):
User, Address, Dingaling = self._o2m_twolevel_fixture(lazy="joined")
class SubDingaling(Dingaling):
pass
mapper(SubDingaling, None, inherits=Dingaling)
lru = Address.dingalings.property._lazy_strategy._bakery(
lambda q: None
)._bakery
l1 = len(lru)
for i in range(5):
sess = Session()
u1 = (
sess.query(User)
.options(
defaultload(User.addresses).lazyload(
Address.dingalings.of_type(aliased(SubDingaling))
)
)
.first()
)
for ad in u1.addresses:
ad.dingalings
l2 = len(lru)
eq_(l1, 0)
eq_(l2, 2)
示例13: test_aliased_bound_are_now_safe_to_cache
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import lazyload [as 别名]
def test_aliased_bound_are_now_safe_to_cache(self):
User, Address, Dingaling = self._o2m_twolevel_fixture(lazy="joined")
class SubDingaling(Dingaling):
pass
mapper(SubDingaling, None, inherits=Dingaling)
lru = Address.dingalings.property._lazy_strategy._bakery(
lambda q: None
)._bakery
l1 = len(lru)
for i in range(5):
sess = Session()
u1 = (
sess.query(User)
.options(
Load(User)
.defaultload(User.addresses)
.lazyload(
Address.dingalings.of_type(aliased(SubDingaling))
)
)
.first()
)
for ad in u1.addresses:
ad.dingalings
l2 = len(lru)
eq_(l1, 0)
eq_(l2, 2)
示例14: test_baked_lazy_loading_m2o
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import lazyload [as 别名]
def test_baked_lazy_loading_m2o(self):
User, Address = self._m2o_fixture()
base_bq = self.bakery(lambda s: s.query(Address))
base_bq += lambda q: q.options(lazyload(Address.user))
base_bq += lambda q: q.order_by(Address.id)
assert_result = self.static.address_user_result
for i in range(4):
for cond1 in (False, True):
bq = base_bq._clone()
sess = Session()
if cond1:
bq += lambda q: q.filter(
Address.email_address == "jack@bean.com"
)
else:
bq += lambda q: q.filter(
Address.email_address.like("ed@%")
)
if cond1:
def go():
result = bq(sess).all()
eq_(assert_result[0:1], result)
self.assert_sql_count(testing.db, go, 2)
else:
def go():
result = bq(sess).all()
eq_(assert_result[1:4], result)
self.assert_sql_count(testing.db, go, 2)
sess.close()
示例15: test_simple_lazy_clause_no_race_on_generate
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import lazyload [as 别名]
def test_simple_lazy_clause_no_race_on_generate(self):
User, Address = self._o2m_fixture()
(
expr1,
paramdict1,
) = User.addresses.property._lazy_strategy._simple_lazy_clause
# delete the attr, as though a concurrent thread is also generating it
del User.addresses.property._lazy_strategy._simple_lazy_clause
(
expr2,
paramdict2,
) = User.addresses.property._lazy_strategy._simple_lazy_clause
eq_(paramdict1, paramdict2)
# additional tests:
# 1. m2m w lazyload
# 2. o2m lazyload where m2o backrefs have an eager load, test
# that eager load is canceled out
# 3. uselist = False, uselist=False assertion
# assert that the integration style illustrated in the dogpile.cache
# example works w/ baked