I've been poking around the Django source as I want to add a column to the changelist view. Specifically, this column is a consolidation of a ManyToMany? relationship. I'll use it as an example here.
In options.py, class ModelAdmin? defines a function that instantiates a Changelist:
class ModelAdmin(BaseModelAdmin):
def __init__(self, model, admin_site):
self.model = model
self.opts = model._meta
self.admin_site = admin_site
#more code
#more code, more functions
def changelist_view(self, request, extra_context=None):
"The 'change list' admin view for this model."
#more code here
from django.contrib.admin.views.main import ChangeList, ERROR_FLAG
#more code here
try:
cl = ChangeList(request, self.model, self.list_display, self.list_display_links, self.list_filter,
self.date_hierarchy, self.search_fields, self.list_select_related, self.list_per_page, self)
#more code here
I propose the class 'ChangeList?' be easily overidable, like so:
from django.contrib.admin.views.main import ChangeList, ERROR_FLAG
class ModelAdmin(BaseModelAdmin):
changelist_class = ChangeList
def __init__(self, model, admin_site):
self.model = model
self.opts = model._meta
self.admin_site = admin_site
self.changelist_class = changelist_class
#more code
def changelist_view(self, request, extra_context=None):
"The 'change list' admin view for this model."
#more code here
from django.contrib.admin.views.main import ChangeList, ERROR_FLAG
#more code here
try:
cl = self.changelist_class(request, self.model, self.list_display, self.list_display_links, self.list_filter,
self.date_hierarchy, self.search_fields, self.list_select_related, self.list_per_page, self)
#more code here
This would allow one to easily override some ChangeList? methods from an admin.py file:
from models import Object, ObjectRelationToWidget #ObjectRelationToWidget acts as the ManyToMany bridge table
from django.contrib.admin.views.main import ChangeList
class ObjectChangeList(ChangeList):
def get_results(self, request):
r = ObjectRelationToWidget.objects.all().select_related() #this puts all the Objects and Widgets into the queryset cache
results = []
for obj in r:
r.object._v_local = {}
r.object._v_local['widget_name'] = r.widget.name
r.object.widget_name = lambda x: return x._v_local['widget_name']
results.append(r.object)
self.result_count = len(results)
self.full_result_count = len(results)
self.result_list = results
class ObjectAdmin(admin.ModelAdmin):
changelist_class = ObjectChangeList
list_display = ["object_name", "widget_name"]
Are there any thoughts on this suggestion? It is simple to add as a feature, but I suppose the usefulness may be seen as somewhat dubious. Suggestions for 'doing this better' (putting a related ManyToMany? field in the admin changelist) would be appreciated, also.