1

I want to change multiple column names in list_display. You could do this with an attribute function using short_description as shown here: Django admin listview Customize Column Name. However, this sounds not very practical if I want to change around 25 column headers.

Is there a more efficient way?

Community
  • 1
  • 1
nrbrt
  • 162
  • 1
  • 10

1 Answers1

0

First workable approach was to generate those 25 attribute functions in the admin init method. However now implemented a much easier approach using a mapping dictionary and a for-loop in JavaScript:

function shorten_column_headers() {
  var headers = {
    'list_display_field_name_1': 'shortened description_1',
    'list_display_field_name_2': 'shortened description_2',
    ...
  }
  for (var field in headers) {
    var $col = $('th.column-' + field + '> div.text > a:nth-child(1)');
    $col.text(headers[field]);
  }
}

$(document).ready(function() {
  shorten_column_headers();
});
nrbrt
  • 162
  • 1
  • 10