Question

I'm using Flask-peewee, looking for a way to give permission to admins, I'd like to make a multi tenancy admin dashboard.

I have made for displaying deals:

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)

So now I'd like to keep the loggedinuserid for Merchant id when they want to edit forms.

enter image description here

*Edit on image text: Merchant_id must be the auth.loggedinid as default

Was it helpful?

Solution

Remove the field from being displayed in the form, then hook into 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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top