Django-tables2: change the text displayed in the column header

I work with a MySQL view (Create View as Select ...) and successfully managed the view's connection to the model as follows:

#models.py class Dashboard(models.Model): devenv = models.CharField(max_length=30, primary_key=True) numberofissues = models.BigIntegerField() class Meta: managed=False db_table = 'stability_dashboard' 

I also managed to display the data in the table using the boiler plate code from the example:

 #tables.py class DashboardTable(tables.Table): class Meta: model = Dashboard attrs = {'class': 'paleblue'} #views.py def dashboard(request): table = DashboardTable(Dashboard.objects.all()) RequestConfig(request).configure(table) return render(request, 'uptime/dash.html', {'table': table}) 

Now I would like to change the title displayed in each column to something more understandable, including spaces, for example. Instead of "devenv" => "development environment"

+4
source share
1 answer

Just add the columns whose names you want to override in tables.py. For instance,

  # tables.py
 import django_tables2 as tables
 from models import Dashboard

 class DashboardTable (tables.Table):
   devenv = tables.Column (verbose_name = 'Development Environment')

   class Meta:
     model = Dashboard
     attrs = {'class': 'paleblue'}

Another (possibly more DRY) solution is to leave tables.py as it is and add verbose_name to the model definition:

  # models.py
 class Dashboard (models.Model):
     devenv = models.CharField (max_length = 30, primary_key = True, verbose_name = 'Development Environment')
     numberofissues = models.BigIntegerField (verbose_name = 'Number of Issues')
     class Meta:
         managed = False
         db_table = 'stability_dashboard'
+3
source

All Articles