Add Excel Download for a Master Web View
So you have a master web view, e.g. for your custom table. Now you might want to add a simple Excel download feature for it.
Enable Excel Download
The base class for master views provides most of the support for Excel download (XLSX file format); one need only enable it to use it with default behavior. This is done by setting an attribute on your master view class:
With that in place, you can filter and sort the grid as you like, then click "Download results as XLSX" link to do just that. Note that this feature requires the corresponding permission to be granted for the user.
Customize Excel File
The default logic does what it can to inspect the columns of the underlying table for the master view. But it is often the case that the grid visible in the web UI will have additional columns from related tables, etc. Default logic does not address this, and in any case you may wish the Excel file to have a different set of columns from the web grid. To customize the download, you must override two methods:
1 class WidgetsView(MasterView): 2 3 results_downloadable_xlsx = True 4 5 def get_xlsx_fields(self): 6 7 # fetch default fields; this will be a list 8 fields = super(WidgetsView, self).get_xlsx_fields() 9 10 # add a field 11 fields.append('foo') 12 13 return fields 14 15 def get_xlsx_row(self, obj, fields): 16 17 # make row dict per default logic 18 row = super(WidgetsView, self).get_xlsx_row(obj, fields) 19 20 # add value for our custom field 21 row['foo'] = 'bar' 22 23 return row