在OpenERP中将不同对象的字段添加到表单中

时间:2014-09-24 15:41:01

标签: python xml openerp openerp-7 odoo

我想将product.product中的字段添加到我新创建的Expense表单(legacy_expense)中,该表单继承了hr.expense.expense。我创建了这个模块来编辑费用管理模块的工作流程 - 我设法做得很好。

我是OpenERP的新手,我很难让它工作。我尝试通过我的.py字段继承表单中的many2one product_id字段,然后让它显示在xml中,但它总是出现错误“XMLSyntaxError:attributes construct error,line 25,column 13”

我猜我的继承是不正确的,我不知道如何将这两个对象联系起来。如果有人可以帮我解决这个问题,那就太好了!

这是我自定义的legacy_expense.py文件:

from openerp.osv import fields, osv

class legacy_expense(osv.osv):


        _inherit = 'hr.expense.expense'

        _columns = {
        'state': fields.selection([

        ('draft', 'New'),
        ('cancelled', 'Refused'),
        ('confirm', 'Waiting Approval'),
        ('done', 'Paid'),

        ], 'Order State', readonly= False, select=True),


        'product_id': fields.many2one('product.product','Product',required=True),


  }

legacy_expense()

这是legacy_expense.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
  <data>
    <record id = "view_expenses_form_custom" model="ir.ui.view">
        <field name="name">view.expenses.form.custom</field>
        <field name="model">hr.expense.expense</field>
        <field name="type">form</field>
        <field name="inherit_id" ref="hr_expense.view_expenses_form" />
        <field name="arch" type="xml">

                <data>
                    <header>
                        <button name="signal_draft_to_confirm" states="draft" string="Submit" type="workflow" groups="base.group_hr_user" />
                    </header>

                <header>
                        <button name="signal_confirm_to_done" states="confirm" string="Approve Expense" type="workflow" groups="base.group_hr_user" />
                    </header>

                <header>
                        <button name="signal_confirm_to_refused" states="confirm" string="Reject Expense" type="workflow" groups="base.group_hr_user" />
                    </header>
                </data>
        <xpath expr = "/form/sheet/group/group[2]/field[@name='user_valid']"position="after">
                <field name="product_id"/>
        </xpath>
        </field>
    </record>
</data>
</openerp>

再次感谢能够提供帮助的任何人!

2 个答案:

答案 0 :(得分:1)

您希望在模型的表单上显示的任何字段都必须位于模型上。在这种情况下,如果要显示产品中的字段,最简单的方法是设置相关字段。

例如,如果您想要显示字段&#34;代码&#34;在product.product上然后添加这样的列。

'product_code': fields.related('product_id', 'code', type = 'char', readonly = True)

然后您可以将视图中的字段用作:

<field name="product_code" />

注意相关字段的第一个参数是模型中引用product.product的many2one字段的名称。

另一件很酷的事情是你可以将这些链接起来,这样你就可以做到:

'currency_name': fields.related('product_id', 'company_id', 'currency_id', 'name', type = 'char', readonly = True)

如果您想在相关表格中引用相关字段(例如many2one),则会有一些问题。在odoo.com上查看开发人员momento中相关字段的文档

答案 1 :(得分:0)

你的继承或py文件没有错。问题出在你的xml文件中,似乎是一个语法错误。我相信它与ur xml文件中的以下行有关

<xpath expr = "/form/sheet/group/group[2]/field[@name='user_valid']"position="after">

在xpath expr和position之间添加一个空格,如下所示:

<xpath expr = "/form/sheet/group/group[2]/field[@name='user_valid']" position="after">

这将解决您的问题。 谢谢和问候