MCPcopy
hub / github.com/tortoise/tortoise-orm / _discover_models

Method _discover_models

tortoise/apps.py:45–69  ·  view source on GitHub ↗
(models_path: ModuleType | str, app_label: str)

Source from the content-addressed store, hash-verified

43
44 @staticmethod
45 def _discover_models(models_path: ModuleType | str, app_label: str) -> list[type[Model]]:
46 if isinstance(models_path, ModuleType):
47 module = models_path
48 else:
49 try:
50 module = importlib.import_module(models_path)
51 except ImportError:
52 raise ConfigurationError(f'Module "{models_path}" not found')
53 discovered_models: list[type[Model]] = []
54 if possible_models := getattr(module, "__models__", None):
55 try:
56 possible_models = [*possible_models]
57 except TypeError:
58 possible_models = None
59 if not possible_models:
60 possible_models = [getattr(module, attr_name) for attr_name in dir(module)]
61 for attr in possible_models:
62 if isclass(attr) and issubclass(attr, Model) and not attr._meta.abstract:
63 if attr._meta.app and attr._meta.app != app_label:
64 continue
65 attr._meta.app = app_label
66 discovered_models.append(attr)
67 if not discovered_models:
68 warnings.warn(f'Module "{models_path}" has no models', RuntimeWarning, stacklevel=4)
69 return discovered_models
70
71 def init_app(
72 self,

Callers 1

init_appMethod · 0.95

Calls 1

ConfigurationErrorClass · 0.90

Tested by

no test coverage detected