22The child admin displays the change/delete view of the subclass model.
33"""
44
5+ from __future__ import annotations
6+
57import inspect
8+ from typing import TYPE_CHECKING , Any
69
710from django .contrib import admin
11+ from django .db import models
12+ from django .forms import ModelForm
13+ from django .http import HttpRequest
814from django .urls import resolve
915from django .utils .translation import gettext_lazy as _
16+ from typing_extensions import TypeVar
1017
1118from polymorphic .utils import get_base_polymorphic_model
1219
1320from ..admin import PolymorphicParentModelAdmin
1421
22+ _ModelT = TypeVar ("_ModelT" , bound = models .Model , default = models .Model )
23+
24+ if TYPE_CHECKING :
25+ _ModelAdminBase = admin .ModelAdmin [_ModelT ]
26+ else :
27+ _ModelAdminBase = admin .ModelAdmin
28+
1529
1630class ParentAdminNotRegistered (RuntimeError ):
1731 "The admin site for the model is not registered."
1832
1933
20- class PolymorphicChildModelAdmin (admin . ModelAdmin ):
34+ class PolymorphicChildModelAdmin (_ModelAdminBase ):
2135 """
2236 The *optional* base class for the admin interface of derived models.
2337
@@ -30,30 +44,32 @@ class PolymorphicChildModelAdmin(admin.ModelAdmin):
3044 """
3145
3246 #: The base model that the class uses (auto-detected if not set explicitly)
33- base_model = None
47+ base_model : type [ models . Model ] | None = None
3448
3549 #: By setting ``base_form`` instead of ``form``, any subclass fields are automatically added to the form.
3650 #: This is useful when your model admin class is inherited by others.
37- base_form = None
51+ base_form : type [ ModelForm [ Any ]] | None = None
3852
3953 #: By setting ``base_fieldsets`` instead of ``fieldsets``,
4054 #: any subclass fields can be automatically added.
4155 #: This is useful when your model admin class is inherited by others.
42- base_fieldsets = None
56+ base_fieldsets : Any = None
4357
4458 #: Default title for extra fieldset
4559 extra_fieldset_title = _ ("Contents" )
4660
4761 #: Whether the child admin model should be visible in the admin index page.
4862 show_in_index = False
4963
50- def __init__ (self , model , admin_site , * args , ** kwargs ) :
64+ def __init__ (self , model : type [ _ModelT ] , admin_site : Any , * args : Any , ** kwargs : Any ) -> None :
5165 super ().__init__ (model , admin_site , * args , ** kwargs )
5266
5367 if self .base_model is None :
5468 self .base_model = get_base_polymorphic_model (model )
5569
56- def get_form (self , request , obj = None , ** kwargs ):
70+ def get_form (
71+ self , request : HttpRequest , obj : Any | None = None , change : bool = False , ** kwargs : Any
72+ ) -> type [ModelForm [Any ]]:
5773 # The django admin validation requires the form to have a 'class Meta: model = ..'
5874 # attribute, or it will complain that the fields are missing.
5975 # However, this enforces all derived ModelAdmin classes to redefine the model as well,
@@ -77,7 +93,7 @@ def get_model_perms(self, request):
7793 return super ().get_model_perms (request )
7894
7995 @property
80- def change_form_template (self ):
96+ def change_form_template (self ) -> list [ str ]: # type: ignore[override]
8197 opts = self .model ._meta
8298 app_label = opts .app_label
8399
@@ -96,7 +112,7 @@ def change_form_template(self):
96112 ]
97113
98114 @property
99- def delete_confirmation_template (self ):
115+ def delete_confirmation_template (self ) -> list [ str ]: # type: ignore[override]
100116 opts = self .model ._meta
101117 app_label = opts .app_label
102118
@@ -115,7 +131,7 @@ def delete_confirmation_template(self):
115131 ]
116132
117133 @property
118- def object_history_template (self ):
134+ def object_history_template (self ) -> list [ str ]: # type: ignore[override]
119135 opts = self .model ._meta
120136 app_label = opts .app_label
121137
0 commit comments