【发布时间】:2014-06-23 13:17:52
【问题描述】:
我正在尝试创建一个新向导,但我不太了解如何做到这一点:( 在一些指南中,我找到了这段代码:
<wizard
id="test_wizard"
keyword="client_action_multi"
model="product.product"
name="product.product.product_id"
string="Test wizard" />
在技术纪念品中我发现了这个:
<record id="action_idea_cleanup_wizard" model="ir.actions.act_window">
<field name="name">Cleanup</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">idea.cleanup.wizard</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
我应该使用哪种语法?
编辑:我已尝试执行向导,代码如下:
python 文件:
from openerp.osv import osv
from openerp.osv import fields
class create_ean(osv.osv_memory):
_name='generate.ean'
_columns = {
'product_def_code' : fields.text('Product Default Code'),
'product_gen_code' : fields.text('Product Generated EAN13', readonly = True),
}
#defaults calls a function that will browse your main object and fill the field with the existing information
_defaults = {
'product_def_code': lambda self, cr, uid, context: self._get_product_id(cr, uid, context),
'product_gen_code': lambda self, cr, uid, context: self.generate_ean(),
}
def save_code(self, cr, uid, ids, context=None):
if 'active_id' in context:
info = self.browse(cr,uid,ids)
self.pool.get('ean13').write(cr,uid,context['active_id'],{'ean13': info[0].product_gen_code})
return {
'type': 'ir.actions.act_window_close',
}
def generate_ean(self):
return "ouhdeguosAB13"
def _get_product_id(self, cr, uid, context=None):
if 'active_id' in context:
return self.pool.get('product.product').browse(cr, uid, context['active_id'], context).product_id
create_ean()
XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<!-- create a view with a unique id -->
<record id="view_generate_ean13_code_wizard" model="ir.ui.view">
<field name="name">generate_ean13_code.form</field>
<field name="model">product.product</field>
<field name="type">form</field>
<field name="arch" type="xml">
<!-- create a normal form view, with the fields you've created on your python file -->
<form string="Insert reformulation info" version="7.0">
<group >
<separator string="EAN13 Code Generator" colspan="2"/>
<field name="product_def_code" string="Product Default Code"/>
<field name="product_gen_code" string="Product EAN13 Generated Code"/>
<newline/>
</group>
<div style="text-align:right">
<button icon="gtk-cancel" special="cancel" string="Cancel"/>
<button icon="gtk-ok" name="save_code" string="Save the code" type="object" />
</div>
</form>
</field>
</record>
<!-- your action window refers to the view_id you've just created -->
<record id="action_generate_ean" model="ir.actions.act_window">
<field name="name">Generate EAN13</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">generate.ean</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="view_id" ref="view_generate_ean13_code_wizard"/>
<field name="target">new</field>
</record>
<act_window id="action_generate_ean"
name="Generate EAN13"
res_model="generate.ean"
view_mode="form"
target="new"
/>
</data>
</openerp>
我将解释我需要做什么,基本上我需要获取产品的 product_id 并生成 ean13,我为代码的生成做了一个测试方法,但我不明白如何保存ean13 字段中生成的代码以及如何使用我的方法通过将 product_id 传递给他来生成该代码
【问题讨论】:
-
那么,我猜您不是在寻找向导,而是想通过单击按钮来调用 python 函数。为此,您可以在 XML 文件表单视图中包含一个按钮。类似于:
<button name="create_ean" string="Create EAN code" type="object" />并在您的 python 中包含一个名为 create_ean 的函数,在该函数中您循环遍历记录并更新每条记录的字段 ean13。 -
好吧,现在我只有 1 个问题,我怎样才能遍历我的记录并更新字段?我看了文档,但没有找到任何东西:(
-
应该这样做:
for prod in self.browse(cr, uid, ids, context=context): my_ean13 = some_algoritm_that_generates_ean_code_based_on_product_id(prod.id) self.write(cr, uid, prod.id, {'ean13': my_ean13 }) -
@FilipeCastanheira 我使用了那个循环,但它一次只适用于一个字段,也许是因为我在表单视图中添加了按钮,但我不知道如何把它放在树视图,如果可能的话
-
你是对的,忘记了从视图表单调用,你正在传递 id。您可以从树形视图中选择产品的 id,或者您可以查看以下答案:stackoverflow.com/questions/16101719/…(然后您可以遍历您的 id!)祝您好运!
标签: python openerp wizard openerp-7 odoo