本文整理汇总了Python中sqlalchemy.orm.joinedload方法的典型用法代码示例。如果您正苦于以下问题:Python orm.joinedload方法的具体用法?Python orm.joinedload怎么用?Python orm.joinedload使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.orm
的用法示例。
在下文中一共展示了orm.joinedload方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_alias_infos_with_pagination
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import joinedload [as 别名]
def get_alias_infos_with_pagination(user, page_id=0, query=None) -> [AliasInfo]:
ret = []
q = (
db.session.query(Alias)
.options(joinedload(Alias.mailbox))
.filter(Alias.user_id == user.id)
.order_by(Alias.created_at.desc())
)
if query:
q = q.filter(
or_(Alias.email.ilike(f"%{query}%"), Alias.note.ilike(f"%{query}%"))
)
q = q.limit(PAGE_LIMIT).offset(page_id * PAGE_LIMIT)
for alias in q:
ret.append(get_alias_info(alias))
return ret
示例2: get_name_for_netloc_db
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import joinedload [as 别名]
def get_name_for_netloc_db(db_sess, netloc):
if netloc in FEED_LOOKUP_CACHE:
return FEED_LOOKUP_CACHE[netloc]
row = db_sess.query(db.RssFeedUrlMapper) \
.filter(db.RssFeedUrlMapper.feed_netloc == netloc) \
.options(joinedload('feed_entry')) \
.all()
if not row:
return False
if len(row) > 1:
print("ERROR: Multiple solutions for netloc %s?" % netloc)
feedname = row[0].feed_entry.feed_name
if feedname:
FEED_LOOKUP_CACHE[netloc] = feedname
return feedname
else:
return False
示例3: renderFeedsTable
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import joinedload [as 别名]
def renderFeedsTable(page=1):
feeds = g.session.query(db.RssFeedPost) \
.order_by(desc(db.RssFeedPost.published))
feeds = feeds.options(joinedload('tag_rel'))
feeds = feeds.options(joinedload('author_rel'))
if feeds is None:
flash('No feeds? Something is /probably/ broken!.')
return redirect(url_for('renderFeedsTable'))
feed_entries = paginate(feeds, page, app.config['FEED_ITEMS_PER_PAGE'])
return render_template('rss-pages/feeds.html',
subheader = "",
sequence_item = feed_entries,
page = page
)
示例4: find_user
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import joinedload [as 别名]
def find_user(self, case_insensitive=False, **kwargs):
from sqlalchemy import func as alchemyFn
query = self.user_model.query
if config_value("JOIN_USER_ROLES") and hasattr(self.user_model, "roles"):
from sqlalchemy.orm import joinedload
query = query.options(joinedload("roles"))
if case_insensitive:
# While it is of course possible to pass in multiple keys to filter on
# that isn't the normal use case. If caller asks for case_insensitive
# AND gives multiple keys - throw an error.
if len(kwargs) > 1:
raise ValueError("Case insensitive option only supports single key")
attr, identifier = kwargs.popitem()
subquery = alchemyFn.lower(
getattr(self.user_model, attr)
) == alchemyFn.lower(identifier)
return query.filter(subquery).first()
else:
return query.filter_by(**kwargs).first()
示例5: get_current_game
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import joinedload [as 别名]
def get_current_game(self, db_session, with_bets=False, with_users=False):
query = db_session.query(HSBetGame).filter(HSBetGame.is_running)
# with_bets and with_users are just optimizations for the querying.
# If a code path knows it's going to need to load the bets and users for each bet,
# we can load them eagerly with a proper SQL JOIN instead of lazily later,
# to make that code path faster
if with_bets:
query = query.options(joinedload(HSBetGame.bets))
if with_users:
query = query.options(joinedload(HSBetGame.bets).joinedload(HSBetBet.user))
current_game = query.one_or_none()
if current_game is None:
current_game = HSBetGame()
db_session.add(current_game)
db_session.flush() # so we get current_game.id set
return current_game
示例6: with_
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import joinedload [as 别名]
def with_(cls, schema):
"""
Query class and eager load schema at once.
:type schema: dict
Example:
schema = {
'user': JOINED, # joinedload user
'comments': (SUBQUERY, { # load comments in separate query
'user': JOINED # but, in this separate query, join user
})
}
# the same schema using class properties:
schema = {
Post.user: JOINED,
Post.comments: (SUBQUERY, {
Comment.user: JOINED
})
}
User.with_(schema).first()
"""
return cls.query.options(*eager_expr(schema or {}))
示例7: with_joined
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import joinedload [as 别名]
def with_joined(cls, *paths):
"""
Eagerload for simple cases where we need to just
joined load some relations
In strings syntax, you can split relations with dot
due to this SQLAlchemy feature: https://goo.gl/yM2DLX
:type paths: *List[str] | *List[InstrumentedAttribute]
Example 1:
Comment.with_joined('user', 'post', 'post.comments').first()
Example 2:
Comment.with_joined(Comment.user, Comment.post).first()
"""
options = [joinedload(path) for path in paths]
return cls.query.options(*options)
示例8: query_orm_for_stop
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import joinedload [as 别名]
def query_orm_for_stop(cls, session, stop_id, detailed=False, agency=None):
"""
simple utility for querying a stop from gtfsdb
"""
ret_val = None
try:
log.info("query Stop for {}".format(stop_id))
q = session.query(cls)
q = q.filter(cls.stop_id == stop_id)
# TODO q.filter(cls.agency_id == agency_id)
if detailed:
try:
q = q.options(joinedload("stop_features"))
except:
pass
ret_val = q.one()
except Exception as e:
log.info(e)
return ret_val
示例9: share_instance_get
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import joinedload [as 别名]
def share_instance_get(context, share_instance_id, session=None,
with_share_data=False):
if session is None:
session = get_session()
result = model_query(
context, models.ShareInstance, session=session,
).filter_by(
id=share_instance_id,
).options(
joinedload('export_locations'),
joinedload('share_type'),
).first()
if result is None:
raise exception.NotFound()
if with_share_data:
parent_share = share_get(context, result['share_id'], session=session)
result.set_share_data(parent_share)
return result
示例10: _share_snapshot_instance_get_with_filters
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import joinedload [as 别名]
def _share_snapshot_instance_get_with_filters(context, instance_ids=None,
snapshot_ids=None, statuses=None,
share_instance_ids=None,
session=None):
query = model_query(context, models.ShareSnapshotInstance, session=session,
read_deleted="no")
if instance_ids is not None:
query = query.filter(
models.ShareSnapshotInstance.id.in_(instance_ids))
if snapshot_ids is not None:
query = query.filter(
models.ShareSnapshotInstance.snapshot_id.in_(snapshot_ids))
if share_instance_ids is not None:
query = query.filter(models.ShareSnapshotInstance.share_instance_id
.in_(share_instance_ids))
if statuses is not None:
query = query.filter(models.ShareSnapshotInstance.status.in_(statuses))
query = query.options(joinedload('share_group_snapshot'))
return query
示例11: _share_type_get_query
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import joinedload [as 别名]
def _share_type_get_query(context, session=None, read_deleted=None,
expected_fields=None):
expected_fields = expected_fields or []
query = (model_query(context,
models.ShareTypes,
session=session,
read_deleted=read_deleted).
options(joinedload('extra_specs')))
if 'projects' in expected_fields:
query = query.options(joinedload('projects'))
if not context.is_admin:
the_filter = [models.ShareTypes.is_public == true()]
projects_attr = getattr(models.ShareTypes, 'projects')
the_filter.extend([
projects_attr.any(project_id=context.project_id)
])
query = query.filter(or_(*the_filter))
return query
示例12: _share_group_snapshot_get
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import joinedload [as 别名]
def _share_group_snapshot_get(context, share_group_snapshot_id, session=None):
session = session or get_session()
result = model_query(
context, models.ShareGroupSnapshot, session=session,
project_only=True, read_deleted='no',
).options(
joinedload('share_group'),
joinedload('share_group_snapshot_members'),
).filter_by(
id=share_group_snapshot_id,
).first()
if not result:
raise exception.ShareGroupSnapshotNotFound(
share_group_snapshot_id=share_group_snapshot_id)
return result
示例13: dashboard
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import joinedload [as 别名]
def dashboard():
latest_docs = [x.id for x in Document.query.order_by(Document.created_at.desc()).limit(30)]
latest_docs = Document.query\
.options(
joinedload('created_by'),
joinedload('sources'),
joinedload('topic'),
joinedload('medium'),
)\
.filter(Document.id.in_(latest_docs))\
.order_by(Document.created_at.desc())
doc_groups = []
for date, group in groupby(latest_docs, lambda d: d.created_at.date()):
doc_groups.append([date, list(group)])
return render_template('dashboard/dashboard.haml',
doc_groups=doc_groups)
示例14: monitor_dashboard
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import joinedload [as 别名]
def monitor_dashboard():
docs = [x.id for x in Document.query
.filter(or_(
Document.created_by_user_id == current_user.id,
Document.checked_by_user_id == current_user.id
))
.order_by(Document.created_at.desc()).limit(30)]
docs = Document.query\
.options(
joinedload('created_by'),
joinedload('sources'),
joinedload('topic'),
joinedload('medium'),
)\
.filter(Document.id.in_(docs))\
.order_by(Document.created_at.desc())
doc_groups = []
for date, group in groupby(docs, lambda d: d.created_at.date()):
doc_groups.append([date, list(group)])
return render_template('dashboard/monitor.haml',
doc_groups=doc_groups)
示例15: api_people
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import joinedload [as 别名]
def api_people():
q = request.args.get('q', '').strip()
try:
limit = max(int(request.args.get('limit', 10)), 0)
except:
limit = 10
if q and request.args.get('similar'):
people = [p for p, _ in Person.similarly_named_to(q, 0.7)]
else:
query = Person.query\
.options(joinedload(Person.affiliation))
if q:
q = '%' + q.replace('%', '%%').replace(' ', '%') + '%'
query = query.filter(Person.name.like(q))\
.order_by(func.length(Person.name))
people = query.order_by(Person.name)\
.limit(limit)\
.all()
return jsonify({'people': [p.json() for p in people]})
# THIS IS A PUBLIC API