|
1 | 1 | from importlib import import_module |
2 | | -from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Type, Union |
| 2 | +from typing import ( |
| 3 | + Any, |
| 4 | + Callable, |
| 5 | + Dict, |
| 6 | + List, |
| 7 | + Optional, |
| 8 | + Sequence, |
| 9 | + Tuple, |
| 10 | + Type, |
| 11 | + Union, |
| 12 | + cast, |
| 13 | +) |
3 | 14 |
|
4 | 15 | from django.core.exceptions import ImproperlyConfigured |
5 | 16 | from django.http import HttpRequest, HttpResponse |
6 | 17 | from django.urls import URLPattern, URLResolver |
7 | | -from django.utils.module_loading import module_has_submodule |
| 18 | +from django.utils.module_loading import import_string, module_has_submodule |
8 | 19 | from ninja import NinjaAPI |
9 | 20 | from ninja.constants import NOT_SET |
10 | 21 | from ninja.openapi.docs import DocsBase, Swagger |
@@ -94,9 +105,14 @@ def urls(self) -> Tuple[List[Union[URLResolver, URLPattern]], str, str]: |
94 | 105 | ) |
95 | 106 |
|
96 | 107 | def register_controllers( |
97 | | - self, *controllers: Union[Type[ControllerBase], Type] |
| 108 | + self, *controllers: Union[Type[ControllerBase], Type, str] |
98 | 109 | ) -> None: |
99 | 110 | for controller in controllers: |
| 111 | + if isinstance(controller, str): |
| 112 | + controller = cast( |
| 113 | + Union[Type[ControllerBase], Type], import_string(controller) |
| 114 | + ) |
| 115 | + |
100 | 116 | if not issubclass(controller, ControllerBase): |
101 | 117 | raise ImproperlyConfigured( |
102 | 118 | f"{controller.__class__.__name__} class is not a controller" |
|
0 commit comments