In Odoo first create the modules and then create a new field that saves images. Now to upload them correctly in your database and to resize images automatically and store them in database you can use the below function or steps I have mentioned:
Step1- In first step we created a new model and the new fields in your own module.
Use this code in .py file show in given below:
class upload_images(osv.osv):
def getimage(self, cr, uid, ids, name, args, context=None):
result = dict.fromkeys(ids, False)
for obj in self.browse(cr, uid, ids, context=context):
result[obj.id] = tools.image_get_resized_images(obj.image)
return result
def setimage(self, cr, uid, id, name, value, args, context=None):
return self.write(cr, uid, [id], {'image': tools.image_resize_image_big(value)}, context=context)
_name = 'upload_images'
_description = 'save and resize'
_columns = {
'name': fields.char('Name', required=True, translate=True),
'image': fields.binary("Image"),
'image_medium': fields.function(getimage, fnct_inv=setimage,
string="Image (auto-resized to 128x128):", type="binary", multi="getimage",
store={
'upload_images.tutorial': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
},),
'image_small': fields.function(getimage, fnct_inv=setimage,
string="Image (auto-resized to 64x64):", type="binary", multi="getimage",
store={
'upload_images.tutorial': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
},
),
}
upload_images()
Step2- In second step we create .xml file and in this file we create view and pass this file in your __openerp__.py file.
Use this code .xml file given below
<?xml version="1.0"?>
<openerp>
<data>
<record id="action_upload_images" model="ir.actions.act_window">
<field name="name">Images</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">upload_images</field>
<field name="view_type">form</field>
<field name="view_id" ref=""/>
<field name="view_mode">tree,form</field>
</record>
<menuitem id="menu_our_customers"
action="action_upload_images" parent="base.menu_sales"
sequence="14"/>
<record id="view_images_form" model="ir.ui.view">
<field name="name">upload_images .form</field>
<field name="model">upload_images</field>
<field name="arch" type="xml">
<form string="images">
<group>
<field name="name"/>
<field name="image_medium"/>
<field name="image_small"/>
</group>
</form>
</field>
</record>
</data>
</openerp>
0 Comment(s)