简体   繁体   English

SQLAlchemy & Flask - 从多表查询中获取结果

[英]SQLAlchemy & Flask - Getting Results from Multi-Table query

Trying to find the solution that does not require adding many conditions and extra code, and haven't been able to find any resources online that tackle my specific need.试图找到不需要添加许多条件和额外代码的解决方案,并且无法在网上找到满足我特定需求的任何资源。

I have a web application that's going to end up being a simple forum and blog site.我有一个 Web 应用程序,它最终将成为一个简单的论坛和博客站点。 There will be individual users and accounts, where each user can either friend or follow other users.将有个人用户和帐户,每个用户可以在其中加好友或关注其他用户。 What is meant to happen is that when you friend and/or follow a user any status update they post is meant to show on your home screen.这意味着当您成为朋友和/或关注用户时,他们发布的任何状态更新都将显示在您的主屏幕上。

However the code only shows the status of those that you are both friends with and following, not if you are only following and not friends and vice versa.但是,该代码仅显示您既是朋友又是关注者的状态,而不是仅关注而不是朋友,反之亦然。 The function that is returning the results is as below -返回结果的函数如下 -

    def related_posts(self):
    shown_posts = db.session.query(Status).join(followers, followers.c.followed_id == Status.user_id).join(
        friends, friends.c.friend_id == Status.user_id).filter(
        followers.c.follower_id == self.id, friends.c.user_id == self.id)
    own_posts = Status.query.filter_by(user_id=self.id)
    return own_posts.union(shown_posts).order_by(Status.timestamp.desc())

This code is part of a User class that creates the User's table.此代码是创建用户表的用户类的一部分。 This code references another class that holds Status' posted by Users, and two sub-tables that are created to facilitate many-to-many relationships for friend and follow functionality.此代码引用另一个类,该类包含用户发布的状态,以及创建两个子表以促进好友和关注功能的多对多关系。

The returned results are shown in the following view -返回的结果显示在以下视图中 -

@bp.route('/', methods=['GET', 'POST'])
@bp.route('/index', methods=['GET', 'POST'])
def index():
    form = StatusForm()
    # if request.method == 'POST' and form.submit():
    if form.validate_on_submit():
        status = Status(body=form.status.data, author=current_user)
        db.session.add(status)
        db.session.commit()
        flash('Your status has been updated!')
        return redirect(url_for('main.index'))
    user_status = Status.query.filter_by(user_id=current_user.id).order_by(Status.timestamp.desc()) if current_user.is_authenticated \
        else Status.query.order_by(Status.timestamp.desc())
    shown_posts = current_user.related_posts() if current_user.is_authenticated \
        else None
    return render_template('main/index.html', title='Welcome to the Blog!', form=form,
                           user_status=user_status, shown_posts=shown_posts)

I don't know if there is a simple way to do this and I'm just missing it, but I've read through the documentation and searched online for a solution to the issue.我不知道是否有一种简单的方法可以做到这一点,我只是想念它,但我已经通读了文档并在线搜索了该问题的解决方案。 But what I've found so far has yet to resolve the issue.但到目前为止我发现的还没有解决这个问题。 Any assistance is greatly appreciated and I hope I presented enough information here to allow that.非常感谢任何帮助,我希望我在这里提供了足够的信息来实现这一点。

This is a very late closure for the question, but I received a great answer from Andi Schroff as a comment on the question:这是一个很晚才结束的问题,但我收到了Andi Schroff 的一个很好的回答,作为对这个问题的评论:

Why not split up shown_posts into shown_posts_friends and shown_posts_follower?为什么不将shown_posts 拆分成shown_posts_friends 和shown_posts_follower? Split up the query for shown_posts into the two parts, and use a union of 3 in the return instead of 2. In your current version your are limiting the outcome to Status of users that are friends and followers.将 show_posts 的查询分成两部分,并在返回中使用 3 而不是 2 的联合。在您当前的版本中,您将结果限制为朋友和关注者用户的状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM