I figured out how to do it by extending the ExporterClass and adapting the TSV exporter to handle csv with the field labels. Perhaps this should be an option for the default ExporterCSV? Here it is for posterity:
class ExporterCSVlabel(ExportClass):
label = 'CSV (real labels)'
file_ext = "csv"
content_type = "text/csv"
def __init__(self, rows):
ExportClass.__init__(self, rows)
def export(self):
out = cStringIO.StringIO()
final = cStringIO.StringIO()
import csv
writer = csv.writer(out)
if self.rows:
import codecs
final.write(codecs.BOM_UTF16)
header = list()
for col in self.rows.colnames:
(t, f) = col.split('.')
field = self.rows.db[t][f]
field_label = field.label # Use the label name instead of database name
colname = unicode(field_label).encode("utf8")
header.append(colname)
writer.writerow(header)
data = out.getvalue().decode("utf8")
data = data.encode("utf-16")
data = data[2:]
final.write(data)
out.truncate(0)
records = self.represented()
for row in records:
writer.writerow(
[str(col).decode('utf8').encode("utf-8") for col in row])
data = out.getvalue().decode("utf8")
data = data.encode("utf-16")
data = data[2:]
final.write(data)
out.truncate(0)
return str(final.getvalue())