The .represent() method and export_to_csv

70 views
Skip to first unread message

Andy W

unread,
Oct 12, 2015, 4:44:27 AM10/12/15
to web2py-users
Hi all

Following the example in the book, I can display the contents of a database table (ie the list of animals in the book example) on the screen and add a button to download the data to a CSV file.

However, changing the model definition to use a reference table:

db.define_table('genus',
   
Field('description'),
    format
= '%(description)s')

db
.define_table('animal',
   
Field('species'),
   
Field('genus', 'reference genus'))

and the controller to use the represent() method:

def animal():
    animals
= db().select(db.animal.ALL).render()
   
return dict(animals=animals)

the view continues to work fine but the export_to_csv button now raises an error:

<type 'exceptions.AttributeError'> 'generator' object has no attribute 'export_to_csv_file'


The ability to export data to csv is important for my application. 

So my question is - is there a (simple) way to export a generator object to a csv file, or is it better to rely on table joins in the DAL rather than the render function?

Any suggestions much appreciated!

Leonel Câmara

unread,
Oct 12, 2015, 5:41:19 AM10/12/15
to web2py-users
export_to_csv_file is actually a method of Rows, when you call render you get a generator that returns the representation for each row, that's not a Rows instance.

You can actually just use export_to_csv_file passing it the argument represent=True and it will use the field's represent.

Andrew Webster

unread,
Oct 12, 2015, 5:56:13 AM10/12/15
to web...@googlegroups.com

Thanks Leonel

On 12 Oct 2015 13:41, "Leonel Câmara" <leonel...@gmail.com> wrote:
export_to_csv_file is actually a method of Rows, when you call render you get a generator that returns the representation for each row, that's not a Rows instance.

You can actually just use export_to_csv_file passing it the argument represent=True and it will use the field's represent.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/z7FXwG9CsO4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages