A very fast way to set up list_display in admin.py

To not waste time and to avoid mistakes.

Testing models is easier through the graphic admin panel Django provides. The mundane thing is to register every models, and, more specifically, to set up list-display. To avoid wasting time, make a ModelAdmin like this:

from django.contrib import admin

class RelationshipAdmin(admin.ModelAdmin):
def get_list_display(self, request):
return [field.name for field in self.model._meta.concrete_fields]

And register all of your models in this fashion, if you are ok with having all fields in list_display:

from django.contrib import admin
from .models import *

@admin.register(WhiteModel)
@admin.register(BlueModel)
@admin.register(RedModel)
class RelationshipAdmin(admin.ModelAdmin):
def get_list_display(self, request):
return [field.name for field in self.model._meta.concrete_fields]

You well have your models registered with all fields – including pk.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.