Odoo Help

Welcome!

This community 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.

0

How to transfer binary files from one model to another in on_change?

By
Yenthe
on 6/30/15, 8:26 AM 504 views

Hi guys,

I have two models, named sale.order.our.customer and sale.order.our.customer.master.
The master contains default values which are uneditable from within other views.
The model sale.order.our.customer has a many2one to sale.order and a many2one to sale.order.our.customer.master like this:

 'our_customer_id': fields.many2one('sale.order', 'Our customer Reference', required=True, ondelete='cascade', select=True),
    'reference_our_customer_id': fields.many2one('sale.order.our.customer.master', string="Reference"),

In the sale.order model I have the following one2many:

'our_customer_line': fields.one2many('sale.order.our.customer', 'our_customer_id', 'About our customer',copy=True), 

The view is defined in XML like this:
 <page name="Customers" string="Customers">
	<field name="our_customer_line">
        <form string="Our customers">
            <field name="reference_our_customer_id" on_change="reference_our_customer_id_change(reference_our_customer_id)"/>
            <field name="name"/>
	    <field name="image"/>
        </form>
	<tree string="Our customers">
	    <field name="name"/>
	    <field name="image"/>
	</tree>
        </field>
	<field name="intro_text_our_customer"/>
</page>

When the user opens the dropdown (which shows all records from sale.order.our.customer.master) and clicks on an item the on_change function will go off. This on_change function will transfer data from the model sale.order.our.customer.master to sale.order.our.customer. This all works fine for default text fields etc but not for binary fields! The code:

 def reference_our_customer_id_change(self, cr, uid, ids, reference_our_customer_id, context=None):
        vals = {'value': {'name': '', 'image': ''}}
        if reference_our_customer_id:
            reference = self.pool.get('sale.order.our.customer.master').browse(cr, uid, reference_our_customer_id, context=context)
            vals = {'value': {'name': reference.name, 'image': reference.image}}
        return vals

The problem is that this throws up an error when I click on the save button:

 MissingError

One of the documents you are trying to access has been deleted, please try again after refreshing.

While I can see in the treeview that the field knows how big the image is etc. When I click on the download file I will get a file with the following name: "sO8013lW.bin".. It seems to contain the base64 encoding.
So my question is.. How do I transfer a file from one model to another in an on_change function , when it is already uploaded in the db? What am I doing wrong?

Thanks,
Yenthe

Yenthe, you are doing the right code. Please try out tools.image_resize_image_big(reference.image) Or decode by base64. Thanks.

Serpent Consulting Services Pvt. Ltd.
on 7/1/15, 4:15 AM

@Serpent by default reference.image seems to be a base64 string. When I use tools.image_resize_image_big I will be able to save the record but when I then want to download it I will get a file in .bin format and when opening the file it will be a base64 string. So why is the filename & extension lost when transferring the data? I would really like to copy over the exact file, the filename and the extensions..

Yenthe
on 7/1/15, 7:14 AM

Yenthe... when odoo stores piuctures in DB it stores base64encoded picture, but you still need v7/v6 stype file_name sotred somewhere... try searching in older versions on how to use that.... basicly look at addons/base/ir/ir_attachment.py ... look at columns.. you have datas_fname ( the part you are missing) and store_fname look for some examples baed o0n this.. hope it helps :)

Bole
on 7/10/15, 2:43 AM
0

Yenthe

--Yenthe--
6676
| 7 7 8
Turnhout, Belgium
--Yenthe--

My Odoo blog: http://www.odoo.yenthevg.com/

My Twitter: https://twitter.com/Yenthe666

Yenthe
On 7/27/15, 4:20 AM

I eventually fixed this with the image_resize_image_big tool. This will automatically convert and handle the file in the correct way. The solution:

 def reference_our_customer_id_change(self, cr, uid, ids, reference_our_customer_id, context=None):
        vals = {'value': {'name': '', 'image': ''}}
        if reference_our_customer_id:
            reference = self.pool.get('sale.order.our.customer.master').browse(cr, uid, reference_our_customer_id, context=context)
            vals = {'value': {'name': reference.name, 'image': tools.image_resize_image_big(reference.image)}}
        return vals

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 6/30/15, 8:26 AM
Seen: 504 times
Last updated: 7/27/15, 4:20 AM