Download file locally in python

2019-04-16 13:16发布

I have a python method in OpenERP that generate a CSV file. My question is how can I set the default path of the CSV will be local path, not a path on the server.

a part of my code have this with open('export_data.csv', 'wb') as csvfile:. which saves the file on the server. I want the file to be saved on the client side location.

Do you have any idea on how to achieve this?

the openerp built in export data function is not an option.

1条回答
混吃等死
2楼-- · 2019-04-16 13:24

Using a fields.binary

One common way to return a binary file to the client side in OpenERP is to use a fields.binary column. You can put it in a TransientModel object (formerly osv_memory, used to create interactive wizards) that lives only for the duration of a session. The binary field value could be set as a result of a previous operation, or may be computed on-the-fly by having a fields.function column simulate it.

You can find several examples of this in the official addons, such as the translation export wizard which produces CVS/PO/TGZ files. One pitfall is that the binary value must be base64 encoded, for historical RPC transfer reasons.

Using a static download URL

As of OpenERP 6.1 it is also possible to offer direct downloads of files from the server filesystem. It is as simple as putting the URL of the file in a fields.char value, as a relative URL rooted in your module, e.g. /module/path/to/file. Then add this field in a form view as a link, using widget="url". One case where this is used is the configuration wizard of the mail client plugins.

This works well for static files that are part of a module, but is not suited for files that are produced by the system as a result of user operations, because they should presumably not be stored on the module's filesystem, even temporarily. On a properly secured installation the OpenERP server process might not even have the right to write to the filesystem on which the module sources are located.


There are other options, such as writing your own web controller, but the above solutions should work well enough for most cases.

查看更多
登录 后发表回答