-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add link for foreign key in display field
- Loading branch information
Showing
5 changed files
with
101 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .custom_table_admin_page import TableData, CustomTableAdminPage | ||
from .extended_admin_model import ExtendedAdminModel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from dataclasses import dataclass | ||
|
||
from django.contrib import admin | ||
from django.shortcuts import render | ||
from django.urls import path | ||
|
||
|
||
@dataclass | ||
class TableData: | ||
header: str | ||
table_titles = [] | ||
table_rows = [] | ||
|
||
def add_rows(self, row: list): | ||
self.table_rows.append(row) | ||
|
||
|
||
class CustomTableAdminPage(admin.ModelAdmin): | ||
model = None | ||
|
||
def get_urls(self): | ||
view_name = '{}_{}_changelist'.format(self.model._meta.app_label, self.model._meta.model_name) | ||
return [ | ||
path('', self.custom_view, name=view_name), | ||
] | ||
|
||
def get_table_data(self): | ||
""" | ||
return list of TableData | ||
""" | ||
raise NotImplementedError() | ||
|
||
def custom_view(self, request, *args, **kwargs): | ||
context = { | ||
**admin.site.each_context(request), | ||
'tables': self.get_table_data(), | ||
} | ||
|
||
return render(request, 'admin/custom/custom_table_page.html', context) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.contrib import admin | ||
|
||
def has_search_fields(field): | ||
model_admin = admin.site._registry.get(field.related_model) | ||
return model_admin and model_admin.search_fields |