OpenERP 向导语法

2 投票
1 回答
6146 浏览
提问于 2025-04-18 10:46

我正在尝试创建一个新的向导,但对怎么做还不是很明白 :(

在一些指南中,我找到了这段代码:

<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。

1 个回答

4

关于向导的文档有点不太清楚,我曾经(现在仍然)对它的工作原理有些困惑,不过我在这里给你提供一个有用的示例,这个示例展示了一个带文本框的向导,它可以把信息保存到一个调用对象中(你需要根据自己的需求修改表的名称)。

首先,创建一个像下面这样的Python文件:

from openerp.osv import osv
from openerp.osv import fields
from openerp.tools.translate import _

class ref_generic_request(osv.osv_memory):

    # create a table in osv_memory, with the columns you need
    # to fill in your wizard
    _name = 'ref.generic.request'

    _columns = {
        'reformulation_info': fields.text(
            'Reformulation instructions',
            help='Instructions for the requestor justification the reformulation needs'),
            }

    # create a function that will save the info from your wizard into
    # your model (active_id is the id of the record you called the wizard
    # from, so you will save the info entered in wizard in that record)
    def save_info(self, cr, uid, ids, context=None):
        if 'active_id' in context:
            info = self.browse(cr, uid, ids)[0].reformulation_info
            gen_req = self.pool.get('generic.request')
            gen_req.write(
                    cr, uid, context['active_id'],
                    {'reformulation_info':info, 'needs_reformulation':1}
                    )
        return {'type':'ir.actions.act_window_close'}
ref_generic_request()

然后创建视图来显示你的向导:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <!-- create a view with a unique id -->
        <record id="view_reformulate_generic_request_wizard" model="ir.ui.view">
            <field name="name">reformulate_generic_request_wizard.form</field>
            <field name="model">ref.generic.request</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="Please insert instruction for the reformulation of this request" colspan="2"/>
                        <field name="reformulation_info" string="Reformulation info"/>
                        <newline/>
                    </group>
                    <div style="text-align:right">
                        <button  icon="gtk-cancel" special="cancel" string="Cancel"/>
                        <button  icon="gtk-ok" name="save_info" string="Send to reformulation" type="object" /> 
                    </div>

               </form>
            </field>
        </record>
        <!-- your action window refers to the view_id you've just created -->
        <record id="action_reformulate_generic_request" model="ir.actions.act_window">
            <field name="name">Reformulate Request</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">ref.generic.request</field>
            <field name="view_type">form</field>
            <field name="view_mode">form</field>
            <field name="view_id" ref="view_reformulate_generic_request_wizard"/>
            <field name="target">new</field> 
        </record>

        <act_window id="action_reformulate_generic_request"
                name="Reformulate Request"
                res_model="ref.generic.request" 
                view_mode="form" 
                target="new"
        />
    </data>
</openerp>

补充说明 - 你可能想用向导来更新已经存储在数据库中的信息。为此,你需要先进行搜索,然后用这些信息加载向导。这是我现在的做法(不确定这是不是最好的方法,我把它放在这里,也许有人会评论并纠正我!)

class review_opinion(osv.osv_memory):
    _name='review.opinion'

    _columns = {
        'opinion_emission': fields.text('Opinion emission'),
        'notes': fields.text('Additional notes'),
    }

    #defaults calls a function that will browse your main object and fill the field with the existing information
    _defaults={
        'opinion_emission': lambda self, cr, uid, context: self._get_opinion(cr, uid, context), 
        'notes': lambda self, cr, uid, context: self._get_notes(cr, uid, context), # populates requestor field with actual user id

    }
    def save_info(self, cr, uid, ids, context=None):
        if 'active_id' in context:
            info = self.browse(cr,uid,ids)
            self.pool.get('opinion').write(cr,uid,context['active_id'],{'opinion_emission': info[0].opinion_emission,'notes': info[0].notes})

        return {
            'type': 'ir.actions.act_window_close',
         }

    #functions that get the info stored in db
    def _get_opinion(self, cr, uid, context=None):
        if 'active_id' in context:
            return self.pool.get('opinion').browse(cr, uid, context['active_id'], context).opinion_emission

    def _get_notes(self, cr, uid, context=None):
        if 'active_id' in context:
            return self.pool.get('opinion').browse(cr, uid, context['active_id'], context).notes

review_opinion()

撰写回答