[Flask_sqlalchemy fuzzy query]

flask_sqlalchemy query methods filter () and filter_ ()

The main difference between these two methods as follows:

Module grammar > <(Above and below) the query and_ and or_ inquiry
filter_by() Directly attribute name = comparison not support not support
filter() With the class name. Attribute name, compare with == stand by stand by

 

To fuzzy query, use the filter () method

inquiry mode:

objs = db_model.query.filter(db_model.api_url.like('%{keyword}%'.format(keyword=keyword))).all()

Written as a function:

    def update_like(self,keyword):
        '''模糊查询'''
        objs = self.db_model.query.filter(self.db_model.api_url.like('%{keyword}%'.format(keyword=keyword))).all()
        obj_num = len(objs)
        for obj in objs:
            if obj.unusual=='True':
                obj_num=obj_num-1
            else:
                obj.unusual='True'
        db.session.commit()

        return '% d records are updated ' % obj_num

 

filter () method supports like, in, not in, and or query

Specific use:

 

Reference documents:

SQLAlchemy in filter_by () and filter () usage difference

 

Guess you like

Origin www.cnblogs.com/kaerxifa/p/11751754.html