Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
default populating of list_display with all the fields
class CommonAdmin(admin.ModelAdmin):
ordering = ['id']
def __init__(self, *args, **kwargs):
super(CommonAdmin, self).__init__(*args, **kwargs)
self.set_list_display(args[0]) # set fields to display --> all of them
def set_list_display(self, model):
fields = []
for field in model._meta.fields:
if field.column == 'id':
continue
elif field.get_internal_type() in ('ForeignKey', 'OneToOneField') and field.column.endswith('_id'):
fields.append(field.column[:-3]) # remove the '_id' to get the relation name !! crazy !
else:
fields.append(field.column)
self.list_display = ('__unicode__', ) + tuple(fields)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.