|
19 | 19 | from polymorphic.utils import get_base_polymorphic_model |
20 | 20 |
|
21 | 21 | from .forms import PolymorphicModelChoiceForm |
22 | | -from .helpers import get_leaf_subclasses |
23 | 22 |
|
24 | 23 |
|
25 | 24 | class RegistrationClosed(RuntimeError): |
@@ -52,13 +51,6 @@ class PolymorphicParentModelAdmin(admin.ModelAdmin): |
52 | 51 | #: The child models that should be displayed |
53 | 52 | child_models = None |
54 | 53 |
|
55 | | - #: The models that should be excluded from the auto-discovered child |
56 | | - #: leaf models that should be displayed. This can be a list of |
57 | | - #: models or a single model. It's useful to exclude non-abstract |
58 | | - #: base models (abstract models are always excluded) when they don't |
59 | | - #: have defined any child models. |
60 | | - exclude_children = None |
61 | | - |
62 | 54 | #: Whether the list should be polymorphic too, leave to ``False`` to optimize |
63 | 55 | polymorphic_list = False |
64 | 56 |
|
@@ -117,41 +109,24 @@ def register_child(self, model, model_admin): |
117 | 109 | def get_child_models(self): |
118 | 110 | """ |
119 | 111 | Return the derived model classes which this admin should handle. |
| 112 | + This should return a list of tuples, exactly like :attr:`child_models` is. |
120 | 113 |
|
121 | | - This should return a list of tuples, exactly like |
122 | | - :attr:`child_models` is. |
123 | | -
|
124 | | - The model classes can be retrieved as |
125 | | - ``base_model.__subclasses__()``, a setting in a config file, or |
126 | | - a query of a plugin registration system at your option |
| 114 | + The model classes can be retrieved as ``base_model.__subclasses__()``, |
| 115 | + a setting in a config file, or a query of a plugin registration system at your option |
127 | 116 | """ |
128 | | - if self.child_models is not None: |
129 | | - return self.child_models |
| 117 | + if self.child_models is None: |
| 118 | + raise NotImplementedError("Implement get_child_models() or child_models") |
130 | 119 |
|
131 | | - child_models = get_leaf_subclasses(self.base_model, self.exclude_children) |
132 | | - |
133 | | - if child_models: |
134 | | - return child_models |
135 | | - |
136 | | - raise ImproperlyConfigured( |
137 | | - f"No child models found for '{self.base_model.__name__}', please " |
138 | | - "define the 'child_models' attribute or overwrite the " |
139 | | - "'get_child_models' method." |
140 | | - ) |
| 120 | + return self.child_models |
141 | 121 |
|
142 | 122 | def get_child_type_choices(self, request, action): |
143 | 123 | """ |
144 | 124 | Return a list of polymorphic types for which the user has the permission to perform the given action. |
145 | 125 | """ |
146 | 126 | self._lazy_setup() |
147 | | - |
148 | | - child_models = self._child_models |
149 | | - if not child_models: |
150 | | - raise ImproperlyConfigured("No child models are available.") |
151 | | - |
152 | 127 | choices = [] |
153 | 128 | content_types = ContentType.objects.get_for_models( |
154 | | - *child_models, for_concrete_models=False |
| 129 | + *self.get_child_models(), for_concrete_models=False |
155 | 130 | ) |
156 | 131 |
|
157 | 132 | for model, ct in content_types.items(): |
|
0 commit comments