简体   繁体   English

如何使用Flask-admin按ID分隔视图模型?

[英]How to separate views model by id with Flask-admin?

I make a tuition payment app with Flask and using Flask-admin as database management for every school who have registered with my app. 我就与支付学费的应用程序和使用瓶管理员数据库管理每一个谁与我的应用程序注册的学校。

I already make it, but now I'm really not sure how to separate data access between every school account who have registered to manage their own tuition payment. 我已经做到了,但是现在我真的不确定如何在已注册管理自己的学费的每个学校帐户之间分隔数据访问。

With Flask-login , I can do it with this code: 使用Flask-login ,我可以使用以下代码:

@app.route('/school_admin')
@login_required
def school_admin():
    school_data = db.session.query(Student.id, Student.name).filter_by(school_id=current_user.id).all()
    return render_template('school_admin_dashboard.html', school_data=school_data)

But, because Flask-admin generates it table views automatically, I'm really not sure how to do that..? 但是,由于Flask-admin自动生成了表格视图,因此我真的不确定如何执行此操作。

So far, I have made my ModelView like this: 到目前为止,我已经将ModelView设置为

class SchoolAdminModelView(sqla.ModelView):
    def is_accessible(self):
        if not current_user.is_active or not current_user.is_authenticated:
            return False
        if current_user.has_role('schooladmin'):
            return True

        return False

    def _handle_view(self, name, **kwargs):
        if not self.is_accessible():
            if current_user.is_authenticated:
                # permission denied
                abort(403)
            else:
                # login
                return redirect(url_for('security.login', next=request.url))

class StudentModelView(SchoolAdminModelView):
    pass

admin.add_view(StudentModelView(Student, db.session))

So, how to separate view model by school_id ..? 那么,如何通过school_id ..分隔视图模型?

NOTE: my reason why am I using Flask-admin instead of Flask-login for school Admin because of it pretty simple to manage user and superuser roles. 注意:我之所以使用Flask-admin而不是Flask-login作为学校管理员,是因为它非常容易管理用户超级用户角色。 And I have using Flask-login for Parent of the student who is the user who consumes this App. 我已经使用Flask-login作为使用此应用程序的用户的学生家长。

Assuming that the logged in user has a school_id connected to them: 假设已登录的用户具有一个连接到他们的school_id

class SchoolAdminModelView(sqla.ModelView):
    def get_query(self):
        return super(SchoolAdminModelView, self).get_query().filter(School.id == current_user.school_id)

It filters the query which is used to generate the list of items in the table. 它过滤用于生成表中项目列表的查询。 You might have to adjust it a bit to your exact needs. 您可能需要根据实际需要对其进行一些调整。

Your code almost works @Joost, but I added a little bit method to it. 您的代码几乎可以使用@Joost,但是我在其中添加了一些方法。

I've found the solution here and modify it a little bit, this is the code how to separate the data access between different school using this way: 我在这里找到了解决方案并对其进行了一些修改,这是代码如何使用这种方式来分隔不同学校之间的数据访问:

class StudentModelView(sqla.ModelView):
    def get_query(self):
        return Student.query.filter_by(school_id=current_user.id)

    def get_count_query(self):
        return self.session.query(func.count('*')).select_from(self.model).filter(
            Student.school_id == current_user.id
        )

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

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