Skip to Content
Menu
This question has been flagged

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

Avatar
Discard

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

Author

@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... 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 :)

Author Best Answer

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
Avatar
Discard
Related Posts Replies Views Activity
2
Jul 22
9897
3
Oct 22
19468
3
Sep 18
11454
0
Jul 16
2992
3
Jan 24
11554