我有一个产品模板形式的向导,我想在向导中为单个产品保存多个图像。在向导中,当我单击“确定”按钮时,所选图像将消失。这是我的代码: -
image.py 代码: -
from openerp import api, fields, models, _
from openerp import SUPERUSER_ID
from openerp.exceptions import UserError
import openerp.addons.decimal_precision as dp
class image_wizard(models.TransientModel):
_name = "image.wizard"
image_med = fields.Many2many('ir.attachment', string="Attachments")
image = fields.Binary("Image")
@api.multi
def action_image_add(self, context=None):
rec = self._context.get('active_ids', [])
if rec:
line_values = {'image_medium':self.image_med
}
product = self.create['product.template'].write(line_values)
image.xml 代码: -
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="view_image_wizard" model="ir.ui.view">
<field name="name">Image wizard</field>
<field name="model">image.wizard</field>
<field name="arch" type="xml">
<form string="Sales Pack">
<group colspan="4" col="4">
<group colspan="4" col="4">
<field name="image_med" widget="many2many_binary" class="thumbimage"/>
<field name="image" invisible="1"/>
</group>
</group>
<footer>
<button name="action_image_add" string="Ok" type="object"
class="btn-primary"/>
<button string="Cancel" class="btn-default" special="cancel"/>
</footer>
</form>
</field>
</record>
<record id="action_view_image_wizard" model="ir.actions.act_window">
<field name="name">Image wizard</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">image.wizard</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="view_id" ref="view_image_wizard"/>
<field name="target">new</field>
</record>
</data>
</openerp>
product.py 代码: -
from openerp import api, fields, models, _
from openerp import SUPERUSER_ID
from openerp.exceptions import UserError
import openerp.addons.decimal_precision as dp
class ProductImage(models.Model):
_inherit ="product.template"
product.xml 代码: -
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="product_image_form" model="ir.ui.view">
<field name="name">product.image.form</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_form_view"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='image_medium']" position="before">
<button name="%(action_view_image_wizard)d" string="see all images" type="action"/>
</xpath>
</field>
</record>
</data>
</openerp>
那么如何在向导中保存多个图像并在向导中查看保存的图像,因为当我们单击“确定”按钮时这些图像会消失。我的代码中有错误吗?有人可以建议吗?
答案 0 :(得分:1)
向导记录并不意味着持久;一段时间后,它们会自动从数据库中删除。这就是他们被称为瞬态的原因。 :doc odoo
答案 1 :(得分:0)
您需要添加一个按钮:
<button string="Open wizard"
name="%(my_module.action_open_wizard)d"
type="action"
class="btn-link"
context="{'image_med': image_med}"/>
在此之后,我们需要将默认值设置为向导:
<record id="action_open_wizard" model="ir.actions.act_window">
<field name="name">action name</field>
<field name="res_model">my_model_two</field>
<field name="view_id" ref="model_two_form_wizard"/>
<field name="context">{'default_image_med': context.get('image_med', False),}</field>
<field name="target">new</field>
</record