Debian Codegraph @

src/polymorphic/tests/admin.py

raw
1 from inspect import isclass
2 from django.contrib.admin import register, ModelAdmin, TabularInline, site as admin_site
3 from django.db.models.query import QuerySet
4 from django.http import HttpRequest
5 from polymorphic.admin import (
6 StackedPolymorphicInline,
7 PolymorphicInlineSupportMixin,
8 PolymorphicChildModelAdmin,
9 PolymorphicChildModelFilter,
10 PolymorphicParentModelAdmin,
11 )
12
13 from polymorphic.tests.models import (
14 PlainA,
15 Model2A,
16 Model2B,
17 Model2C,
18 Model2D,
19 InlineModelA,
20 InlineModelB,
21 InlineParent,
22 NoChildren,
23 ModelWithPolyFK,
24 M2MAdminTest,
25 M2MAdminTestChildA,
26 M2MAdminTestChildB,
27 M2MAdminTestChildC,
28 M2MThroughBase,
29 M2MThroughProject,
30 M2MThroughPerson,
31 M2MThroughMembership,
32 M2MThroughMembershipWithPerson,
33 M2MThroughMembershipWithSpecialPerson,
34 M2MThroughProjectWithTeam,
35 M2MThroughSpecialPerson,
36 DirectM2MContainer,
37 )
38
39
40 @register(Model2A)
41 class Model2Admin(PolymorphicParentModelAdmin):
42 list_filter = (PolymorphicChildModelFilter,)
43 child_models = (Model2A, Model2B, Model2C, Model2D)
44
45
46 admin_site.register(Model2B, PolymorphicChildModelAdmin)
47 admin_site.register(Model2C, PolymorphicChildModelAdmin)
48
49
50 @register(Model2D)
51 class Model2DAdmin(PolymorphicChildModelAdmin):
52 exclude = ("field3",)
53
54
55 @register(PlainA)
56 class PlainAAdmin(ModelAdmin):
57 search_fields = ["field1"]
58
59 def get_queryset(self, request: HttpRequest) -> QuerySet:
60 return super().get_queryset(request).order_by("pk")
61
62
63 class Inline(StackedPolymorphicInline):
64 model = InlineModelA
65
66 def get_child_inlines(self):
67 return [
68 child
69 for child in self.__class__.__dict__.values()
70 if isclass(child) and issubclass(child, StackedPolymorphicInline.Child)
71 ]
72
73 class InlineModelAChild(StackedPolymorphicInline.Child):
74 model = InlineModelA
75
76 class InlineModelBChild(StackedPolymorphicInline.Child):
77 model = InlineModelB
78 autocomplete_fields = ["plain_a"]
79
80
81 @register(InlineParent)
82 class InlineParentAdmin(PolymorphicInlineSupportMixin, ModelAdmin):
83 inlines = (Inline,)
84 extra = 1
85
86
87 @register(NoChildren)
88 class NoChildrenAdmin(PolymorphicParentModelAdmin):
89 child_models = (NoChildren,)
90
91
92 @register(ModelWithPolyFK)
93 class ModelWithPolyFKAdmin(ModelAdmin):
94 fields = ["name", "poly_fk"]
95
96
97 @register(M2MAdminTest)
98 class M2MAdminTestAdmin(PolymorphicParentModelAdmin):
99 list_filter = (PolymorphicChildModelFilter,)
100 child_models = (M2MAdminTestChildA, M2MAdminTestChildB, M2MAdminTestChildC)
101
102
103 @register(M2MAdminTestChildA)
104 class M2MAdminTestChildA(PolymorphicChildModelAdmin):
105 raw_id_fields = ("child_bs",)
106
107
108 @register(M2MAdminTestChildB)
109 class M2MAdminTestChildB(PolymorphicChildModelAdmin):
110 raw_id_fields = ("child_as",)
111
112
113 @register(M2MAdminTestChildC)
114 class M2MAdminTestChildC(PolymorphicChildModelAdmin):
115 raw_id_fields = ("child_as",)
116
117
118 # Issue #182: M2M field in model admin
119 # Register models to test M2M field to polymorphic model
120 @register(M2MThroughBase)
121 class M2MThroughBaseAdmin(PolymorphicParentModelAdmin):
122 """Base admin for polymorphic M2M test models."""
123
124 child_models = (
125 M2MThroughProject,
126 M2MThroughPerson,
127 M2MThroughProjectWithTeam,
128 M2MThroughSpecialPerson,
129 )
130
131
132 @register(M2MThroughProject)
133 class M2MThroughProjectAdmin(PolymorphicChildModelAdmin):
134 """Admin for M2MThroughProject polymorphic child."""
135
136 pass
137
138
139 @register(M2MThroughPerson)
140 class M2MThroughPersonAdmin(PolymorphicChildModelAdmin):
141 """Admin for M2MThroughPerson polymorphic child."""
142
143 pass
144
145
146 @register(M2MThroughSpecialPerson)
147 class M2MThroughSpecialPersonAdmin(PolymorphicChildModelAdmin):
148 """Admin for M2MThroughSpecialPerson polymorphic child."""
149
150 pass
151
152
153 @register(DirectM2MContainer)
154 class DirectM2MContainerAdmin(ModelAdmin):
155 """
156 Test case for Issue #182: M2M field in model admin.
157 DirectM2MContainer has a direct M2M field to polymorphic M2MThroughBase model.
158 This should work without AttributeError: 'int' object has no attribute 'pk'.
159 """
160
161 filter_horizontal = ("items",)
162
163
164 # Issue #375: Admin with M2M through table on polymorphic model
165 class M2MThroughMembershipInline(StackedPolymorphicInline):
166 """
167 Polymorphic inline for Issue #375: M2M through table with polymorphic membership types.
168 This tests creating different membership types inline based on person type.
169 """
170
171 model = M2MThroughMembership
172 extra = 1
173
174 class MembershipWithPersonChild(StackedPolymorphicInline.Child):
175 """Inline for regular Person membership."""
176
177 model = M2MThroughMembershipWithPerson
178
179 class MembershipWithSpecialPersonChild(StackedPolymorphicInline.Child):
180 """Inline for SpecialPerson membership with special notes."""
181
182 model = M2MThroughMembershipWithSpecialPerson
183
184 child_inlines = (
185 MembershipWithPersonChild,
186 MembershipWithSpecialPersonChild,
187 )
188
189
190 @register(M2MThroughProjectWithTeam)
191 class M2MThroughProjectWithTeamAdmin(PolymorphicInlineSupportMixin, PolymorphicChildModelAdmin):
192 """
193 Test case for Issue #375: Admin with M2M through table on polymorphic model.
194 M2MThroughProjectWithTeam (polymorphic) has M2M to M2MThroughPerson (polymorphic)
195 with custom through model M2MThroughMembership (now polymorphic).
196 Uses polymorphic inlines to support different membership types.
197 """
198
199 inlines = (M2MThroughMembershipInline,)