如何编辑flask-admin以支持多租户?

时间:2014-03-17 17:57:01

标签: python flask flask-admin flask-peewee

我正在使用Flask-peewee,寻找一种授予管理员权限的方法,我想制作一个多租户管理仪表板。

我已经做了显示交易:

class DealsAdmin(ModelAdmin):
    columns = ('deal_name', 'deal_desc', 'created_on')
    exclude = ('created_on','merchand_id')

    def get_query(self):
        loggedin_username=auth.get_logged_in_user()
        merchant=Merchant.select().where(Merchant.id == loggedin_username).get()
        return self.model.select().where(self.model.merchand_id == loggedin_username)

所以现在我想在他们想要编辑表单时保留logininuserid for Merchant id。

enter image description here

*在图片文字上修改:Merchant_id必须是auth.loggedinid默认

1 个答案:

答案 0 :(得分:2)

删除表单中显示的字段,然后挂钩到on_model_change:

class MyDealModelView(ModelView):
    form_excluded_columns = ('merchant_id',)

    def on_model_change(form, model, is_created):
        model.merchant_id = login.current_user.merchant_id;

http://flask-admin.readthedocs.org/en/latest/api/mod_model/#flask.ext.admin.model.BaseModelView.on_model_change