@@ -43,6 +43,14 @@ class AdminModelListView(AdminListView):
43
43
list_order = []
44
44
search_fields : list = ["pk" ]
45
45
46
+ @classmethod
47
+ def get_title (cls ) -> str :
48
+ return cls .model ._meta .verbose_name_plural .capitalize ()
49
+
50
+ @classmethod
51
+ def get_slug (cls ) -> str :
52
+ return cls .model ._meta .model_name
53
+
46
54
def get_context (self ):
47
55
context = super ().get_context ()
48
56
@@ -92,122 +100,90 @@ def get_object_field(self, object, field: str):
92
100
return get_model_field (object , field )
93
101
94
102
95
- class AdminModelViewset :
103
+ class AdminModelDetailView ( AdminDetailView ) :
96
104
model : "models.Model"
97
- list_description = ""
98
- list_fields : list = ["pk" ]
99
- list_order = []
100
- detail_fields : list = []
101
- search_fields = ["pk" ]
102
-
103
- form_class = None # TODO type annotation
104
-
105
- list_cards = []
106
- form_cards = []
105
+ fields : list = []
107
106
108
107
@classmethod
109
- def get_list_view (cls ) -> AdminModelListView :
110
- class V (AdminModelListView ):
111
- model = cls .model
112
- title = cls .model ._meta .verbose_name_plural .capitalize ()
113
- description = cls .list_description
114
- slug = cls .model ._meta .model_name
115
- list_fields = cls .list_fields
116
- list_order = cls .list_order
117
- cards = cls .list_cards
118
- search_fields = cls .search_fields
108
+ def get_title (cls ) -> str :
109
+ return cls .model ._meta .verbose_name .capitalize ()
119
110
120
- def get_update_url (self , object ):
121
- update_view = cls .get_update_view ()
111
+ @classmethod
112
+ def get_slug (cls ) -> str :
113
+ return f"{ cls .model ._meta .model_name } _detail"
122
114
123
- if not update_view :
124
- return None
115
+ @classmethod
116
+ def get_path (cls ) -> str :
117
+ return f"{ cls .model ._meta .model_name } /<int:pk>/"
125
118
126
- # TODO a way to do this without explicit namespace?
127
- return reverse_lazy (
128
- URL_NAMESPACE + ":" + update_view .view_name (),
129
- kwargs = {"pk" : object .pk },
130
- )
119
+ def get_context (self ):
120
+ context = super ().get_context ()
121
+ context ["fields" ] = self .fields or ["pk" ] + [
122
+ f .name for f in self .object ._meta .get_fields () if not f .remote_field
123
+ ]
124
+ return context
131
125
132
- def get_detail_url (self , object ):
133
- detail_view = cls . get_detail_view ( )
126
+ def get_object_field (self , object , field : str ):
127
+ return get_model_field ( object , field )
134
128
135
- if not detail_view :
136
- return None
129
+ def get_object ( self ) :
130
+ return self . model . objects . get ( pk = self . url_kwargs [ "pk" ])
137
131
138
- return reverse_lazy (
139
- URL_NAMESPACE + ":" + detail_view . view_name (),
140
- kwargs = { "pk" : object . pk } ,
141
- )
132
+ def get_template_names ( self ) -> list [ str ]:
133
+ return super (). get_template_names () + [
134
+ "admin/detail.html" ,
135
+ ]
142
136
143
- def get_initial_queryset (self ):
144
- return cls .get_list_queryset (self )
145
137
146
- return V
138
+ class AdminModelUpdateView (AdminUpdateView ):
139
+ model : "models.Model"
140
+ form_class = None # TODO type annotation
141
+ success_url = "." # Redirect back to the same update page by default
147
142
148
143
@classmethod
149
- def get_update_view (cls ) -> AdminUpdateView | None :
150
- if not cls .form_class :
151
- return None
144
+ def get_title (cls ) -> str :
145
+ return f"Update { cls .model ._meta .verbose_name } "
152
146
153
- class V (AdminUpdateView ):
154
- title = f"Update { cls .model ._meta .verbose_name } "
155
- slug = f"{ cls .model ._meta .model_name } _update"
156
- form_class = cls .form_class
157
- path = f"{ cls .model ._meta .model_name } /<int:pk>/update/"
158
- cards = cls .form_cards
159
- success_url = "." # Redirect back to the same update page by default
160
- parent_view_class = cls .get_list_view ()
147
+ @classmethod
148
+ def get_slug (cls ) -> str :
149
+ return f"{ cls .model ._meta .model_name } _update"
161
150
162
- def get_object (self ):
163
- return cls .model .objects .get (pk = self .url_kwargs ["pk" ])
151
+ @classmethod
152
+ def get_path (cls ) -> str :
153
+ return f"{ cls .model ._meta .model_name } /<int:pk>/update/"
164
154
165
- return V
155
+ def get_object (self ):
156
+ return self .model .objects .get (pk = self .url_kwargs ["pk" ])
166
157
167
- @classmethod
168
- def get_detail_view (cls ) -> AdminDetailView | None :
169
- class V (AdminDetailView ):
170
- title = cls .model ._meta .verbose_name .capitalize ()
171
- slug = f"{ cls .model ._meta .model_name } _detail"
172
- path = f"{ cls .model ._meta .model_name } /<int:pk>/"
173
- parent_view_class = cls .get_list_view ()
174
- fields = cls .detail_fields
175
-
176
- def get_context (self ):
177
- context = super ().get_context ()
178
- context ["fields" ] = self .fields or ["pk" ] + [
179
- f .name for f in self .object ._meta .get_fields () if not f .remote_field
180
- ]
181
- return context
182
-
183
- def get_object (self ):
184
- return cls .model .objects .get (pk = self .url_kwargs ["pk" ])
185
-
186
- def get_template_names (self ) -> list [str ]:
187
- return super ().get_template_names () + [
188
- "admin/detail.html" ,
189
- ]
190
-
191
- def get_object_field (self , object , field : str ):
192
- return get_model_field (object , field )
193
-
194
- return V
195
158
159
+ class AdminModelViewset :
196
160
@classmethod
197
161
def get_views (cls ) -> list ["View" ]:
198
162
views = []
199
163
200
- if list_view := cls .get_list_view ():
201
- views .append (list_view )
164
+ if hasattr (cls , "ListView" ) and hasattr (cls , "DetailView" ):
165
+ cls .ListView .get_detail_url = lambda self , obj : reverse_lazy (
166
+ f"{ URL_NAMESPACE } :{ cls .DetailView .view_name ()} " ,
167
+ kwargs = {"pk" : obj .pk },
168
+ )
202
169
203
- if update_view := cls .get_update_view ():
204
- views .append (update_view )
170
+ cls .DetailView .parent_view_class = cls .ListView
205
171
206
- if detail_view := cls .get_detail_view ():
207
- views .append (detail_view )
172
+ if hasattr (cls , "ListView" ) and hasattr (cls , "UpdateView" ):
173
+ cls .ListView .get_update_url = lambda self , obj : reverse_lazy (
174
+ f"{ URL_NAMESPACE } :{ cls .UpdateView .view_name ()} " ,
175
+ kwargs = {"pk" : obj .pk },
176
+ )
208
177
209
- return views
178
+ cls . UpdateView . parent_view_class = cls . ListView
210
179
211
- def get_list_queryset (self ):
212
- # Can't use super() with this the way it works now
213
- return self .model .objects .all ()
180
+ if hasattr (cls , "ListView" ):
181
+ views .append (cls .ListView )
182
+
183
+ if hasattr (cls , "DetailView" ):
184
+ views .append (cls .DetailView )
185
+
186
+ if hasattr (cls , "UpdateView" ):
187
+ views .append (cls .UpdateView )
188
+
189
+ return views
0 commit comments