Skip to Content
Menu
This question has been flagged
2 Replies
11993 Views

How to create multiple images with its preview in product template? Here is my code :-


from openerp import models, fields, api


class product_image(models.Model):
_name = 'product.image'
_order = 'sequence, id DESC'

name = fields.Char('Name')
description = fields.Text('Description')
sequence = fields.Integer('Sequence')
image_alt = fields.Text('Image Label')
image = fields.Binary('Image')
image_small = fields.Binary('Small Image')
product_tmpl_id = fields.Many2one('product.template', 'Product', select=True)
from_main_image = fields.Boolean('From Main Image', default=False)


class product_product(models.Model):
_inherit = 'product.product'

images = fields.One2many('product.image', related='product_tmpl_id.images',
string='Images', store=False)


class product_template(models.Model):
_inherit = 'product.template'

images = fields.One2many('product.image', 'product_tmpl_id',
string='Images')

@api.one
def action_copy_image_to_images(self):
if not self.image:
return
image = None
for r in self.images:
if r.from_main_image:
image = r
break

if
image:
image.image = self.image
else:
vals = {'image': self.image,
'name': self.name,
'product_tmpl_id': self.id,
'from_main_image': True, }
self.env['product.image'].create(vals)

xml code:-
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="product_product_template_only_form_view" model="ir.ui.view">
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_form_view"/>
<field name="arch" type="xml">
<xpath expr="//notebook[1]" position="inside">
<page string="Product Images" name="website_mutli_image">
<field name="images" context="{'default_name': name}">
<tree editable="bottom">
<field name="sequence" widget="handle"/>
<field name="name"/>
<field name="description"/>
<field name="image_alt"/>
<field name="image" widget="image" height="64"/>
</tree>
</field>
</page>
</xpath>
</field>
</record>
<record id="product_product_product_only_form_view" model="ir.ui.view">
<field name="model">product.product</field>
<field name="inherit_id" ref="product.product_normal_form_view"/>
<field name="arch" type="xml">
<xpath expr="//notebook[1]" position="inside">
<page string="Product Images" name="website_mutli_image">
<field name="images" mode="kanban" context="{'default_name': name}">
<kanban>
<field name="name"/>
<field name="description"/>
<field name="image_alt"/>
<field name="image"/>
<templates>
<t t-name="kanban-box">
<div style="position: relative">
<div class="oe_module_vignette">
<a type="open">
<img t-att-src="kanban_image('res.partner', 'image', record.id.value, {'preview_image': 'image_small'})" class="oe_avatar oe_kanban_avatar_smallbox"/>
</a>
<div class="oe_module_desc">
<div class="oe_kanban_box_content oe_kanban_color_bglight oe_kanban_color_border">
<table class="oe_kanban_table">
<tr>
<td class="oe_kanban_title1" align="left" valign="middle">
<h4><a type="open"><field name="name"/></a></h4>
<i><div t-if="record.description.raw_value">
<field name="description"/></div></i>
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
</t>
</templates>
</kanban>
</field>
</page>
</xpath>
</field>
</record>

</data>
</openerp>
Avatar
Discard
Best Answer

you just install the e-commerce, and there is the option for multiple images in products. please refer it, it will help you.

https://www.odoo.com/documentation/user/10.0/ecommerce/managing_products/multi_images.html

Avatar
Discard
Best Answer

You can use this module http://bit.ly/2yOKX5o. This can be the answer for your question

Avatar
Discard
Related Posts Replies Views Activity
1
Oct 17
2265
2
Mar 24
10322
5
Sep 20
5181
1
Dec 19
8410
4
Jan 19
7786