diff --git a/backend/app/__init__.py b/backend/app/__init__.py index d0139d09..7fb80ae0 100644 --- a/backend/app/__init__.py +++ b/backend/app/__init__.py @@ -3,7 +3,7 @@ import os.path from backend.core.path_conf import BASE_PATH -from backend.utils.import_parse import get_model_object +from backend.utils.import_parse import get_model_objects def get_app_models() -> list[type]: @@ -21,9 +21,9 @@ def get_app_models() -> list[type]: for app in apps: module_path = f'backend.app.{app}.model' - obj = get_model_object(module_path) + obj = get_model_objects(module_path) if obj: - objs.append(obj) + objs.extend(obj) return objs diff --git a/backend/plugin/tools.py b/backend/plugin/tools.py index 3a285dfe..f71dad45 100644 --- a/backend/plugin/tools.py +++ b/backend/plugin/tools.py @@ -23,7 +23,7 @@ from backend.core.conf import settings from backend.core.path_conf import PLUGIN_DIR from backend.database.redis import RedisCli, redis_client from backend.utils._await import run_await -from backend.utils.import_parse import get_model_object, import_module_cached +from backend.utils.import_parse import get_model_objects, import_module_cached class PluginConfigError(Exception): @@ -63,9 +63,9 @@ def get_plugin_models() -> list[type]: for plugin in get_plugins(): module_path = f'backend.plugin.{plugin}.model' - obj = get_model_object(module_path) + obj = get_model_objects(module_path) if obj: - objs.append(obj) + objs.extend(obj) return objs diff --git a/backend/utils/import_parse.py b/backend/utils/import_parse.py index 2bf0bb58..22769acd 100644 --- a/backend/utils/import_parse.py +++ b/backend/utils/import_parse.py @@ -39,7 +39,7 @@ def dynamic_import_data_model(module_path: str) -> Type[T]: raise errors.ServerError(msg='数据模型列动态解析失败,请联系系统超级管理员') -def get_model_object(module_path: str) -> type | None: +def get_model_objects(module_path: str) -> list[type] | None: """ 获取模型对象 @@ -52,10 +52,12 @@ def get_model_object(module_path: str) -> type | None: log.warning(f'模块 {module_path} 中不包含模型对象') return None except Exception as e: - raise RuntimeError(f'获取模块 {module_path} 模型对象失败:{e}') + raise e + + classes = [] for name, obj in inspect.getmembers(module): - if inspect.isclass(obj): - return obj + if inspect.isclass(obj) and module_path in obj.__module__: + classes.append(obj) - return None + return classes