2

Table structure like -

db.define_table('parent',
   Field('name'),format='%(name)s')

db.define_table('children',
   Field('name'),
   Field('mother','reference parent'),
   Field('father','reference parent'))
db.children.mother.requires = IS_IN_DB(db, db.parent.id,'%(name)s')
db.children.father.requires = IS_IN_DB(db, db.parent.id,'%(name)s')

Controller :

grid = SQLFORM.grid(db.children, orderby=[db.children.id],
                        csv=True, 
                        fields=[db.children.id, db.children.name, db.children.mother, db.children.father])

    return dict(grid=grid)   

Here grid shows proper values i.e names of the mother and father from the parent table. But when I try to export it via csv link - resulted excelsheet shows ids and not the names of mother and father.

Please help!

1 Answer 1

2

The CSV download just gives you the raw database values without first applying each field's represent attribute. If you want the "represented" values of each field, you have two options. First, you can choose the TSV (tab-separated-values) download instead of CSV. Second, you can define a custom export class:

import cStringIO

class CSVExporter(object):
    file_ext = "csv"
    content_type = "text/csv"

    def __init__(self, rows):
        self.rows = rows

    def export(self):
        if self.rows:
            s = cStringIO.StringIO()
            self.rows.export_to_csv_file(s, represent=True)
            return s.getvalue()
        else:
            return ''

grid = SQLFORM.grid(db.mytable, exportclasses=dict(csv=(CSVExporter, 'CSV')))

The exportclasses argument is a dictionary of custom download types that can be used to override existing types or add new ones. Each item is a tuple including the exporter class and the label to be used for the download link in the UI.

We should probably add this as an option.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.