-1

What does "pdf, _ = ..." mean (Odoo 11) in this controller?

# odoo\addons\website_sale\controllers\main.py
@http.route(['/shop/print'], type='http', auth="public", website=True)
def print_saleorder(self):
    sale_order_id = request.session.get('sale_last_order_id')
    if sale_order_id:
        pdf, _ = request.env.ref('sale.action_report_saleorder').sudo().render_qweb_pdf([sale_order_id])
        pdfhttpheaders = [('Content-Type', 'application/pdf'), ('Content-Length', u'%s' % len(pdf))]
        return request.make_response(pdf, headers=pdfhttpheaders)
    else:
        return request.redirect('/shop')

If I remove the " , _ " and leave only the variable "pdf = ...", the download of any report in Odoo website doesn't work.

I'd like to understand what it stands for.

2 Answers2

1

The expression you're calling returns a tuple, not a single value. The pdf, _ = is called tuple unpacking - it takes the (in this case two item) tuple's values and stores the first one in the first variable, and the second one in the second variable. So pdf gets set to the first element in the tuple, and _ is set to the second.

The _ isn't a special character or anything, it's just a convention for a variable you don't actually care about, but the syntax requires an identifier there.

Chris Tavares
  • 29,165
  • 4
  • 46
  • 63
1

It looks like the expression returns a tuple containing two elements. The pdf, _ = means unpack the tuple assigning the first value to pdf and the second value to _.

Among other uses underscore's are often used as variable names when you don't care about, or will not use, the contents of the variable but python requires an identifier.

sedders123
  • 791
  • 1
  • 9
  • 19