database_model_manage.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # coding=utf-8
  2. """
  3. @project: MaxKB
  4. @Author:虎虎
  5. @file: database_model_manage.py
  6. @date:2025/4/15 11:06
  7. @desc:
  8. """
  9. from importlib import import_module
  10. from django.conf import settings
  11. def new_instance_by_class_path(class_path: str):
  12. """
  13. 根据class_path 创建实例
  14. """
  15. parts = class_path.rpartition('.')
  16. package_path = parts[0]
  17. class_name = parts[2]
  18. module = import_module(package_path)
  19. HandlerClass = getattr(module, class_name)
  20. return HandlerClass()
  21. class DatabaseModelManage:
  22. """
  23. 模型字典
  24. """
  25. model_dict = {}
  26. @staticmethod
  27. def get_model(model_name):
  28. """
  29. 根据模型
  30. """
  31. return DatabaseModelManage.model_dict.get(model_name)
  32. @staticmethod
  33. def init():
  34. handles = [new_instance_by_class_path(class_path) for class_path in
  35. (settings.MODEL_HANDLES if hasattr(settings, 'MODEL_HANDLES') else [])]
  36. for h in handles:
  37. model_dict = h.get_model_dict()
  38. DatabaseModelManage.model_dict = {**DatabaseModelManage.model_dict, **model_dict}