【问题标题】:How to correctly inherit a class from sale module and change fields如何正确从销售模块继承类并更改字段
【发布时间】:2018-01-18 14:57:03
【问题描述】:

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

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>

我将此代码添加到 ma​​nifest.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

请帮忙。 谢谢。

【问题讨论】:

  • 我在你的问题中找到了答案:D
  • 对你有好处:)

标签: python xml inheritance odoo-10 odoo


【解决方案1】:

如果我对您有很好的理解,您想创建一个新的自定义产品类别并在销售订单行中替换旧的产品类别。因此,您已经明确定义了 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-11
    相关资源
    最近更新 更多