How to customize flask admin QuerySelectMultipleFi

2019-09-16 10:27发布

I am using flask_admin to allow admin user to access database, where one-to-many relationship is presented. When editing an entry, I would like the dropdown menu to show only the options that meet the condition. I though query_factory can do this. The following is minimal example of what I have now:

class OneSideObj(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    active = db.Column(db.Boolean)
    many_side_obj_id = db.Column(
        db.Integer,
        db.ForeignKey('many_side_obj.id')
    )

class ManySideObj(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    one_side_objs = db.relationship(
        'OneSideObj',
        backref = 'many_side_obj',
        lazy = 'dynamic',
    )

def get_manysideobj_id(*args):
    ##################
    # how to get id? #
    ##################
    return id

class ManySideObjView(ModelView):
    id = get_manysideobj_id(*some_args) # <--- get current many_side_obj id

    form_args = {
        'one_side_objs': {
            'query_factory': lambda: query_filter(id)
        }
    }

def query_filter(id):
    return OneSideObj.query.filter(
        (OneSideObj.many_side_obj_id == id) | (OneSideObj.active == False)
    )

The query I would like the field to display is all inactive OneSideObj, or active ones but with the matching many_side_obj_id. I am stuck with by getting current model instance ID. It seems to be a simple task but I can't find the way to do it. Or maybe this approach is not doable?

2条回答
唯我独甜
2楼-- · 2019-09-16 11:05

You're right that the model instance isn't available in the query_factory, but it's not actually necessary to have a function to pass to that field. You can instead just create a query and pass it directly to the form. This avoids the instance variable and can be done in a single method.

class ManySideObjView(ModelView):
    def edit_form(self, obj):
        form = super(ManySideObjView, self).edit_form(obj)

        query = self.session.query(OneSideObj).filter(
                    (OneSideObj.active == False) | (OneSideObj.many_side_obj_id == obj.id))

        form.one_side_objs.query = query
        return form
查看更多
Animai°情兽
3楼-- · 2019-09-16 11:11

After hours of try/error and googling, it seems this is almost what I want to do. The problem boils down to the fact that edit_form will not be called when ModelView initiated. Therefore I believe there is no direct way to access the model instance that is being edited. The work around is to overwrite edit_form and hijact the obj variable where the model instance is being passed. This is the code working for me (based on the minimized example)

class ManySideObjView(ModelView):

    # model instance is passed to edit_form as obj
    def edit_form(self, obj):
        return self._filtered(
            super(ManySideObjView, self).edit_form(obj), obj.id
        )

    # save id in self._instance_id and access it from _get_list
    def _filtered(self, form, id):
        self._instance_id = id
        form.one_side_objs.query_factory = self._get_list
        return form

    # actual query logic
    def _get_list(self):
        id = self._instance_id

        return self.session.query(OneSideObj).filter(
            (OneSideObj.active == False) | (OneSideObj.many_side_obj_id == id)
        )
查看更多
登录 后发表回答