MaxKB/apps/common/database_model_manage/database_model_manage.py
shaohuzhang1 04642eb497
Some checks are pending
sync2gitee / repo-sync (push) Waiting to run
feat: authentication
2025-04-15 20:37:38 +08:00

45 lines
1.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# coding=utf-8
"""
@project: MaxKB
@Author虎虎
@file database_model_manage.py
@date2025/4/15 11:06
@desc:
"""
from importlib import import_module
from django.conf import settings
def new_instance_by_class_path(class_path: str):
"""
根据class_path 创建实例
"""
parts = class_path.rpartition('.')
package_path = parts[0]
class_name = parts[2]
module = import_module(package_path)
HandlerClass = getattr(module, class_name)
return HandlerClass()
class DatabaseModelManage:
"""
模型字典
"""
model_dict = {}
@staticmethod
def get_model(model_name):
"""
根据模型
"""
return DatabaseModelManage.model_dict.get(model_name)
@staticmethod
def init():
handles = [new_instance_by_class_path(class_path) for class_path in
(settings.MODEL_HANDLES if hasattr(settings, 'MODEL_HANDLES') else [])]
for h in handles:
model_dict = h.get_model_dict()
DatabaseModelManage.model_dict = {**DatabaseModelManage.model_dict, **model_dict}