如何正确继承销售模块中的类并更改字段

时间:2018-01-18 14:57:03

标签: python xml inheritance odoo-10 odoo

我正在尝试继承“销售”模块并使用我自己的产品类。所以我创建了这个类:

class mymodule_product(models.Model):
    _name = "mymodule.product"
    _description = "mymodule Product Description"

    name = fields.Char('Description', required= True)
    code = fields.Integer('Code', required= True)
    category_id = fields.Many2one('mymodule.category','Category')

我创建了另一个具有我要将其添加到销售订单行的字段的类:

class mymoduleSaleOrder(models.Model):
    _name = 'mymodule.sale.order'   
    mymodule_product_id = fields.Many2one('mymodule.product', string='Products', required=True)

然后我创建了这个xml代码:

<record id="mymodule_sale_order_form_view" model="ir.ui.view">
        <field name="name">mymodule.sale_order.form.view</field>
        <field name="model">mymodule.sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <field name="product_id" position="replace">
                <field name="mymodule_product_id" />
            </field>
        </field>
    </record>

我将此代码添加到清单 .py:

'depends': ['base', 'sale'],

当我升级模块时出现此错误:

File "..\odoo\addons\base\ir\ir_ui_view.py", line 380, in write
return super(View, self).write(self._compute_defaults(vals))
File "..\odoo\models.py", line 3557, in write
self._write(old_vals)
File "..\odoo\models.py", line 3708, in _write
self._validate_fields(vals)
File "..\odoo\models.py", line 1079, in _validate_fields
raise ValidationError("%s\n\n%s" % (_("Error while validating constraint"), tools.ustr(e)))
ParseError: "Error while validating constraint

Field `origin` does not exist

Error context:
View `mymodule.sale_order.form.view`
[view_id: 934, xml_id: n/a, model: mymodule.sale.order, parent_id: 539]
None" while parsing file:.../views/sale_order.xml:4, near
    <record id="mymodule_sale_order_form_view" model="ir.ui.view">
        <field name="name">mymodule.sale_order.form.view</field>
        <field name="model">mymodule.sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <field name="product_id" position="replace">
                <field name="mymodule_product_id"/>
            </field>
        </field>
    </record>

当我尝试使用这个python代码时:

class mymoduleSaleOrder(models.Model):
    _name = 'mymodule.sale.order' 
    _inherit = 'sale.order'  
    mymodule_product_id = fields.Many2one('mymodule.product', string='Products', required=True)

我收到了这个错误:

ParseError: "Error while validating constraint

Field `randa_product_id` does not exist

请帮助。 感谢。

1 个答案:

答案 0 :(得分:0)

如果我很了解你,你想要创建一个新的定制产品类别,并在销售订单行中替换旧产品。因此,您已经明确定义了mymodule.product类,但您不必为销售订单和销售订单行创建新类,只需从现有订单行继承:

class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'   

    mymodule_product_id = fields.Many2one(
        comodel_name='mymodule.product',
        string='Products',
        required=True
    )

然后,您的XML代码必须与此类似(我建议您不要替换原始字段product_id,只需隐藏它。我还建议您使用xpath,因为有几个例子原始视图中的product_id字段,如果不是,则只修改最后一个字段):

<record id="mymodule_sale_order_form_view" model="ir.ui.view">
    <field name="name">mymodule.sale_order.form.view</field>
    <field name="model">sale.order</field>
    <field name="inherit_id" ref="sale.view_order_form"/>
    <field name="arch" type="xml">
        <xpath expr="//field[@name='order_line']/form//field[@name='product_id']" position="attributes">
            <attribute name="invisible">1</attribute>
        </xpath>
        <xpath expr="//field[@name='order_line']/form//field[@name='product_id']" position="after">
            <field name="mymodule_product_id" />
        </xpath>
        <xpath expr="//field[@name='order_line']/tree/field[@name='product_id']" position="attributes">
            <attribute name="invisible">1</attribute>
        </xpath>
        <xpath expr="//field[@name='order_line']/tree/field[@name='product_id']" position="after">
            <field name="mymodule_product_id" />
        </xpath>
    </field>
</record>