如何从销售模块正确继承 class 并更改字段
How to correctly inherit a class from sale module and change fields
我正在尝试继承 "sale" 模块并使用我自己的产品 class。所以我创建了这个 class:
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,其中包含我要将其添加到销售订单行的字段:
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>
我将此代码添加到 manifest.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
请帮忙。
谢谢。
如果我理解您的意思,您想要创建新的自定义 class 产品并替换销售订单行中的旧产品。因此,您的 mymodule.product
class 定义明确,但您不必为销售订单或销售订单行创建新的 class,只需继承现有订单即可:
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>
我正在尝试继承 "sale" 模块并使用我自己的产品 class。所以我创建了这个 class:
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,其中包含我要将其添加到销售订单行的字段:
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>
我将此代码添加到 manifest.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
请帮忙。 谢谢。
如果我理解您的意思,您想要创建新的自定义 class 产品并替换销售订单行中的旧产品。因此,您的 mymodule.product
class 定义明确,但您不必为销售订单或销售订单行创建新的 class,只需继承现有订单即可:
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>