Skip to content

Commit d1cdb9e

Browse files
authored
Fix the get model object method return logic (#832)
* Fix the get model object method return * Update the error trigger method * Delete the error log
1 parent 6c029b9 commit d1cdb9e

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

backend/app/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os.path
44

55
from backend.core.path_conf import BASE_PATH
6-
from backend.utils.import_parse import get_model_object
6+
from backend.utils.import_parse import get_model_objects
77

88

99
def get_app_models() -> list[type]:
@@ -21,9 +21,9 @@ def get_app_models() -> list[type]:
2121

2222
for app in apps:
2323
module_path = f'backend.app.{app}.model'
24-
obj = get_model_object(module_path)
24+
obj = get_model_objects(module_path)
2525
if obj:
26-
objs.append(obj)
26+
objs.extend(obj)
2727

2828
return objs
2929

backend/plugin/tools.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from backend.core.path_conf import PLUGIN_DIR
2424
from backend.database.redis import RedisCli, redis_client
2525
from backend.utils._await import run_await
26-
from backend.utils.import_parse import get_model_object, import_module_cached
26+
from backend.utils.import_parse import get_model_objects, import_module_cached
2727

2828

2929
class PluginConfigError(Exception):
@@ -63,9 +63,9 @@ def get_plugin_models() -> list[type]:
6363

6464
for plugin in get_plugins():
6565
module_path = f'backend.plugin.{plugin}.model'
66-
obj = get_model_object(module_path)
66+
obj = get_model_objects(module_path)
6767
if obj:
68-
objs.append(obj)
68+
objs.extend(obj)
6969

7070
return objs
7171

backend/utils/import_parse.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def dynamic_import_data_model(module_path: str) -> Type[T]:
3939
raise errors.ServerError(msg='数据模型列动态解析失败,请联系系统超级管理员')
4040

4141

42-
def get_model_object(module_path: str) -> type | None:
42+
def get_model_objects(module_path: str) -> list[type] | None:
4343
"""
4444
获取模型对象
4545
@@ -52,10 +52,12 @@ def get_model_object(module_path: str) -> type | None:
5252
log.warning(f'模块 {module_path} 中不包含模型对象')
5353
return None
5454
except Exception as e:
55-
raise RuntimeError(f'获取模块 {module_path} 模型对象失败:{e}')
55+
raise e
56+
57+
classes = []
5658

5759
for name, obj in inspect.getmembers(module):
58-
if inspect.isclass(obj):
59-
return obj
60+
if inspect.isclass(obj) and module_path in obj.__module__:
61+
classes.append(obj)
6062

61-
return None
63+
return classes

0 commit comments

Comments
 (0)