Skip to content

Commit a53604f

Browse files
author
Cuong Nguyen
committed
update custom table page
1 parent 9b0e355 commit a53604f

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

admin_extended/base.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import copy
2+
from dataclasses import dataclass
23

34
from django.contrib import messages
45
from django.contrib import admin
@@ -12,6 +13,15 @@ def has_search_fields(field):
1213
model_admin = admin.site._registry.get(field.related_model)
1314
return model_admin and model_admin.search_fields
1415

16+
@dataclass
17+
class TableData:
18+
header: str
19+
table_titles = []
20+
table_rows = []
21+
22+
def add_rows(self, row: list):
23+
self.table_rows.append(row)
24+
1525

1626
class ExtendedAdminModel(ObjectToolModelAdminMixin, UIUtilsMixin, admin.ModelAdmin):
1727
"""
@@ -146,25 +156,16 @@ def get_urls(self):
146156
path('', self.custom_view, name=view_name),
147157
]
148158

149-
def get_data(self):
159+
def get_table_data(self):
150160
"""
151-
return dict of list. Eg:
152-
results = {
153-
'title': ['A', 'B', 'C'],
154-
'rows': [
155-
[12, 12, 14],
156-
[12, 12, 14],
157-
[12, 12, 14],
158-
[12, 12, 14],
159-
],
160-
}
161+
return list of TableData
161162
"""
162163
raise NotImplementedError()
163164

164165
def custom_view(self, request, *args, **kwargs):
165166
context = {
166167
**admin.site.each_context(request),
167-
'data': self.get_data(),
168+
'tables': self.get_table_data(),
168169
}
169170

170171
return render(request, 'admin/custom/custom_table_page.html', context)

admin_extended/templates/admin/custom/custom_table_page.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
<div class="module filtered" id="changelist">
1919
<div class="changelist-form-container">
2020
<div class="results">
21-
<table id="result_list">
21+
{% for table in tables %}
22+
<h2>{{ table.header }}</h2>
23+
<table class="result_list" style="margin-bottom: 40px">
2224
<thead>
2325
<tr>
24-
{% for title in data.title %}
26+
{% for title in table.table_titles %}
2527
<th scope="col" class="">
2628
<div class="text"><span>{{ title }}</span></div>
2729
<div class="clear"></div>
@@ -30,7 +32,7 @@
3032
</tr>
3133
</thead>
3234
<tbody>
33-
{% for row in data.rows %}
35+
{% for row in table.table_rows %}
3436
<tr>
3537
{% for item in row %}
3638
<td class="">{{ item }}</td>
@@ -39,6 +41,7 @@
3941
{% endfor %}
4042
</tbody>
4143
</table>
44+
{% endfor %}
4245
</div>
4346
</div>
4447

0 commit comments

Comments
 (0)