Odoo Help

2

0
10 Answers
8
Avatar

Emipro Technologies Pvt. Ltd. India

--Emipro Technologies Pvt. Ltd.--
7760
| 7 8 10
Rajkot, India
--Emipro Technologies Pvt. Ltd.--

Emipro has been providing a wide range of business solutions in the domain of ERP & eCommerce. We take pride in serving 1000s of contented customers across the globe with strong roots in the US, Europe & India. Being a Certified Odoo Gold Partner in India as well as the United States, we bring our rich professional experience and unrivaled technical competence to help our customers obtain efficient and cost-effective business solutions in Odoo. With our team of 50+ Odoo Experts and highly experienced business analysts, we serve our customers which are companies of all sizes, ranging from start-ups to large enterprises. 

Our ERP Services:

  • Business Need Analysis & GAP Analysis

  • ERP Migration

  • Odoo Consultancy

  • Odoo Implementation & Customization

  • Odoo Training & Support

  • Odoo Website & eCommerce Development

  • Odoo Integration (Marketplaces, eCommerce platforms, Shipping Carriers, Payment Gateways etc.)

Our Achievements:


Reach Us:

Website: www.emiprotechnologies.com

Email: info@emiprotechnologies.com

Skype: emiprotech

Emipro Technologies Pvt. Ltd. India
11/2/15, 12:27 AM

Hi,

We at Emipro, got this question numerous times from different Odoo developers. So here I am giving the step by step guidance how to do this.

Here all my code is in V8.

  • Create a method inside your regular model and return URL. 

For example, 

 @api.multi 
def get_stock_file(self):
    return {
            'type' : 'ir.actions.act_url',
            'url': '/web/binary/download_document?model=wizard.product.stock.report&field=datas&id=%s&filename=product_stock.xls'%(self.id),
            'target': 'self',
}

Here I have returned URL with model and some info. Now in next step i will catch that URL to a controller method.

( just like file you can see in /web/controllers/main.py )

  • Make a controller class and catch that url and do process for download excel file.

from openerp import http
from openerp.http import request
from openerp.addons.web.controllers.main import serialize_exception,content_disposition
import base64
class Binary(http.Controller):
@http.route('/web/binary/download_document', type='http', auth="public")
@serialize_exception
def download_document(self,model,field,id,filename=None, **kw):
    """ Download link for files stored as binary fields.
    :param str model: name of the model to fetch the binary from
     :param str field: binary field
     :param str id: id of the record from which to fetch the binary
    :param str filename: field holding the file's name, if any
    :returns: :class:`werkzeug.wrappers.Response`
    """
    Model = request.registry[model]
    cr, uid, context = request.cr, request.uid, request.context
    fields = [field]
    res = Model.read(cr, uid, [int(id)], fields, context)[0]
    filecontent = base64.b64decode(res.get(field) or '')
    if not filecontent:
         return request.not_found()
    else:
        if not filename:
            filename = '%s_%s' % (model.replace('.', '_'), id)
            return request.make_response(filecontent,
                            [('Content-Type', 'application/octet-stream'),
                             ('Content-Disposition', content_disposition(filename))])

In above method I have got the ID from url and then applied some calculation and return the http response from request. Whatever values I have passed from wizard to controller method, I will get them on controller method.

( See below, I have passed model, field, id and filename from url )

/web/binary/download_document?model=wizard.product.stock.report&field=datas&id=%s&filename=product_stock.xls
def download_document(self,model,field,id,filename=None, **kw):

I have returned Excel file but you can return any kind of file and even an attachment from database binary field too.

By apply code like above, you will able to return any file on button click without intervention of binary field.

I hope this answer will add extra spice into your knowledge and make your Odoo technical understanding more delicious.

Odoo Technical notes is published for this. You can view How to download any file on button click ?  and review it.

2 Comments
E.M. Spain
11/8/15, 8:38 AM

This is super detailed and extremely useful. I have pending to try in my project as we need it to generate custom printing files for a thermal printer. Thanks a lot.

Sarina Nepal
7/20/17, 3:03 AM

Does this code work for odoo10 as well . I got error while using these codes.

res = Model.read(model,cr, uid, [int(id)], fields, context)[0]

TypeError: unbound method read() must be called with kyc.clients instance as first argument (got unicode instance instead)

2
Avatar

Akhil P Sivan India

--Akhil P Sivan--

4209
| 6 4 9
Kochi, India
--Akhil P Sivan--

Odoo Developer

email-id : akhilpsivan01@gmail.com

Akhil P Sivan India
11/1/15, 4:48 PM

Hi,

You may try using a Binary field.

file = fields.Binary(string='File')
1 Comment
Sehrish Pakistan
3/22/18, 3:04 AM

Thanks it works in my case. I use binary field in some of my tasks, have a look: http://learnopenerp.blogspot.com/2016/06/using-binary-fields-in-odoo.html

0
chesucr Spain
9/11/19, 11:27 AM
This works in Odoo 11 at least. If you have a binary field in any model:

@api.multi
def crete_and_download_dummy_field(self):

# store here your binary file into the dummy field

return {
'name': 'FEC',
'type': 'ir.actions.act_url',
'url': '/web/content/?model=sale.order&id={}&field=dummy&filename_field=dummy_filename&download=true'.format(
self.id
),
'target': 'self',
}

If you just want a link to download it in a form you can create a binary file and its name

mock_pdf = fields.Binary(string='Mock pdf')

mock_pdf_filename = fields.Char(
string='Mock PDF Filename',
compute='_compute_mock_pdf_filename'
)

@api.depends('mock_pdf')
def _compute_mock_pdf_filename(self):
self.ensure_one()
name = self.name.replace('/', '_')
name = name.replace('.', '_')
name = name + '.pdf'
self.mock_pdf_filename = name

And in the form

<field name="mock_pdf_filename" invisible="1" />
<field name="mock_pdf" filename="mock_pdf_filename" />
0
E.M. Spain
1/27/16, 4:06 PM

This works fine but I do not fully understand this part of the code:

Model = request.registry[model]     
cr, uid, context = request.cr, request.uid, request.context     
fields = [field]
res = Model.read(cr, uid, [int(id)], fields, context)[0]

What does Model contain?

What does fields contain?

What does res contain?

1 Comment
David Yao Hong Kong
4/24/18, 2:34 AM

hey, I got the same problem, did you figure out how?

Ask a Question
Writer
413
E.M.
Keep Informed
4 follower(s)
About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register
Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now