1
votes

I have two binary fields. In the sale order line tree view, I have a button to open my wizard. I chose and save file and it's perfectly saved in my label_file field of the selected sale order line.

The problem is that when I open the wizard I want to see it as a saved file, but it's not generated and only bytes are in the path.

first

Class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'

    label_file = fields.Binary('Saved Label')

Saved in SaleOrderLine

enter image description here

second

class OrderLineLabel(models.TransientModel):
    _name = 'order.line.label'


label_file_show = fields.Binary('Label file')


@api.multi
def write_label_vals(self):
    self.ensure_one()
    sale_order_line = self.env['sale.order.line'].browse(self.env.context.get('active_ids'))
    vals = {

        'label_file': self.label_file,

    }
    sale_order_line.write(vals)


@api.model
def default_get(self, fields):
    res = super(OrderLineLabel, self).default_get(fields)
    order_line_id = self.env['sale.order.line'].browse(self.env.context.get('active_ids'))

    status, headers, content = binary_content(model='sale.order.line', field='label_file', id=order_line_id.id,filename='test',download=True)
    #tried like this
    res['label_file_show'] = content

    #or just like this
    res['label_file_show'] = order_line_id.label_file

    return res

this is how it looks when I open the wizard.

enter image description here

1

1 Answers

0
votes

You need to add filename to the binary fields.

Declare a char field to hold the name:

label_file_name = fields.Char()

And use filename attribute to specify the file name to the binary field:

<field name="label_file_name" invisible="True"/>
<field name="label_file_show" filename="label_file_name"/>

In write_label_vals add one more line to save also the file name.

order_line_id.label_file_name = self.label_file_name  
# vals = {'label_file': self.label_file, 'label_file_name': self.label_file_name}

Set the value of the filename in the wizard default_get method :

res['label_file_name'] = order_line_id.label_file_name