I think the "colnames" argument to export_to_csv_file is just to specify
which particular columns to include in the export, not to rename the
columns in the output. When it gets to a column name that isn't one of the
fields, it checks in the record's "_extra" attribute (which is used to
store aggregates like sum and count), and not finding an "_extra" attribute
in this case raises a KeyError exception.
I'm not sure you can easily change the column names. You might set
write_columns=False (so the column names don't get written at all), and
then add your own row of column names at the top of the file.
Anthony
On Saturday, March 17, 2012 4:42:58 AM UTC-4, Rey wrote:
>
> Hi there,
>
> I've spent hours and tried everything under the sun, I'm new to web2py
> but loving the framework.
>
> I want to be able to set the exported csv header row titles to not be "
> table.id, table.name etc."
> but rather a proper label "ID, Name" etc.
>
> This is part of the code I'm using:
>
> *from gluon.contenttype import contenttype
>> ** response.headers['Content-Type'] = contenttype('.csv')
>> ** response.headers['Content-disposition'] = 'attachment;
>> filename=%s_database.csv' % (
>> ** request.now
>> ** )
>> ** import csv, cStringIO
>> ** s = cStringIO.StringIO()*
>
>
>
> *colnames=['id','name']*
>
> *records.export_to_csv_file(s, delimiter=',', quotechar='"',
>> quoting=csv.QUOTE_NONNUMERIC, colnames=colnames)*
>
> *return s.getvalue()*
>
>
>
> *
> *
> The error I keep getting is something to do with an "_extra" :
>
> *Traceback (most recent call last):
>> ** File "/Users/Rey/Dropbox/web2py/gluon/restricted.py", line 204, in
>> restricted
>> ** exec ccode in environment
>> ** File
>> "/Users/Rey/Dropbox/web2py/applications/example/controllers/account.py",
>> line 187, in <module>
>> ** File "/Users/Rey/Dropbox/web2py/gluon/globals.py", line 172, in
>> <lambda>
>> ** self._caller = lambda f: f()
>> ** File
>> "/Users/Rey/Dropbox/web2py/applications/example/controllers/account.py",
>> line 183, in export
>> ** records.export_to_csv_file(s, delimiter=',', quotechar='"',
>> quoting=csv.QUOTE_NONNUMERIC, colnames=colnames)
>> ** File "/Users/Rey/Dropbox/web2py/gluon/dal.py", line 6669, in
>> export_to_csv_file
>> ** row.append(record._extra[col])
>> ** File "/Users/Rey/Dropbox/web2py/gluon/dal.py", line 4440, in
>> __getattr__
>> ** return self[key]
>> ** File "/Users/Rey/Dropbox/web2py/gluon/dal.py", line 4431, in
>> __getitem__
>> ** return dict.__getitem__(self, key)
>> **KeyError: '_extra'*
>
>
> Version of Web2py I'm using is Version 1.99.4 (2011-12-14 14:46:14) stable
>
> Any help would be much appreciated!
>