释解:
# *maps为序列解包
# add_entity多表关系查询使用,在结果集增加关联表的实体类数据,如果后续不使用关联表的数据,则可不进行定义
# getattr获取对象属性值信息,适合**kwargs为不定长参数时去获取实体类的对象,以及属性值同传入的参数进行校验
# filter多参数时建议使用and,格式:filter(and_(*maps, user.delete == "N"))
# func.count,使用数据统计类时,用法为func.sum,func.count,记得尾部带上scalar()
def queryUserRecord(pagNo, pageSize, **kwargs): maps = [] pagNo = pageSize * (pagNo - 1) user= User merchant = Merchant for i in kwargs.keys(): if kwargs[i]: # getattr获取对象属性值信息,getattr(对象,获取i的属性值) == 属性值 maps.append(getattr(user, i) == kwargs[i]) try: item_filter = session.query(user).join(merchant,user.application_name==merchant.app_name)./ add_entity(merchant).order_by(user.create_time.desc())./ filter(and_(*maps, user.delete == "N")).offset(pagNo).limit(pageSize).all() totalRows = session.query(func.count(user.id)).filter(and_(*maps, user.delete == "N")).scalar() except Exception as e: session.close() finally: session.close() test_item = [] for i in item_filter: user= to_json(i[0]) merchant= to_json(i[1]) user["deploy_link"] = merchant["deploy_link"] test_item.append(user) dict_res = {"totalRows": totalRows, "list": test_item} session.close() print("maps",maps) return dict_res if __name__ == '__main__': print(queryUserRecord(1,100,serviceGroup=45))
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/279639.html