# 初始化数据创建、只需要调用一次的接口 import json from Context.common import common_db from Mods.CommonInformation.Mods.IndustryMain.Models import IndustryMain from Mods.CommonInformation.Mods.ProductMain.Models import ProductMain from Utils.PrintUtils import print_error from Utils.SqlAlchemyUtils import tree_table_to_json def save_industry_main_json(): """ 保存行业分类数据 """ db = common_db.get_db_i() try: tree = [] for item in db.query(IndustryMain).filter_by(type='一级行业').all(): tree.append({"text": item.name, "value": item.code}) # tree = tree_table_to_json(db, IndustryMain, key_str='code') json.dump({'data': tree}, open('static/industry_main_use.json', 'w', encoding="utf-8"), ensure_ascii=False, ) except Exception as e: print_error(e) finally: db.close() def save_product_main_json(): """ 保存产品分类数据 """ db = common_db.get_db_i() try: # tree = [] # for item in db.query(ProductMain).filter_by(type='一级分类').all(): # tree.append({"text": item.name, "value": item.code}) tree = tree_table_to_json(db, ProductMain, key_str='code') # json.dump({'data': tree}, open('static/product_main_use.json', 'w', encoding="utf-8"), ensure_ascii=False, ) except Exception as e: print_error(e) finally: db.close() save_industry_main_json() save_product_main_json()