【问题标题】:Inherited field does not exist in odooodoo中不存在继承的字段
【发布时间】:2022-01-19 17:29:58
【问题描述】:

我正在为 odoo 15 中的销售报价单开发一个自定义插件,同时继承 sale.order.template 模型。我正在尝试在数量字段旁边添加一个新字段,但我不断收到与我的视图文件相关的“字段 [字段名称] 不存在错误”。 这是我的视图文件中的代码:

<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<data>
<record id="sales_quotation_form_inherit" model="ir.ui.view">
<field name="name">sale.order.template.form.inherit</field>
<field name="model">sale.order.template</field>
<field name="inherit_id" ref="sale_management.sale_order_template_view_form"/>
 <field name="arch" type="xml">
    <xpath expr="//field[@name='sale_order_template_line_ids']/form[@string='Quotation Template Lines']/group/group[1]/div/field[@name='product_uom_qty']" positon="after">
     <field name='price'/>
     </xpath>
</field>
</record>
</data>
</odoo>

还有我的 model.py 代码:

from odoo import models, fields

class SalesQuotation(models.Model):
    _inherit = "sale.order.template"
    price = fields.Many2one(string='Unit Price')

有人可以为我指出可能是什么问题的正确方向吗?

【问题讨论】:

    标签: python xml xpath odoo odoo-15


    【解决方案1】:

    您的新字段是Many2one。 您需要指定它引用哪个表,即:

    price = fields.Many2one('other.table...', string='Unit Price')

    你也不能只使用float 字段吗?在任何情况下,我都会先用一个普通的 float 字段进行测试。

    【讨论】:

    • 我不认为这是解决他的问题的方法,但是是的,价格字段的 many2one 似乎是错误的。无论如何我都会投票:D
    【解决方案2】:

    您的视图扩展似乎改变了销售模板的行。所以你的模型扩展是为错误的模型完成的。改为扩展sale.order.template.line

    from odoo import models, fields
    
    class SaleOrderTemplateLine(models.Model):
        _inherit = "sale.order.template.line"
        price = fields.Float(string='Unit Price')
    

    哦,价格不应该是浮动的吗?我已经在我的代码示例中更改了它。

    【讨论】:

    • 大概就是这样。 OP 正在访问该行中的字段。
    【解决方案3】:

    解决问题的方法很少:

    视图继承

    1. 确保您的自定义模块依赖于定义了sale.order.templatesale_management 模块:

    清单.py

    ...
    'depends': ['sale_management'],
    ...
    
    1. 简化xpath
    ...
    <xpath expr="//field[@name='sale_order_template_line_ids']/form//field[@name='product_uom_qty']" positon="after">
        <field name='price' />
    </xpath>
    ...
    

    型号

    把你的代码改成这样:

    from odoo import models, fields
    
    class SalesQuotation(models.Model):
        _inherit = "sale.order.template"
        price = fields.Float(string='Unit Price')
    

    【讨论】:

    • 这里也一样:确实是好点,但不能解决问题。
    猜你喜欢
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多