Odoo 10 - 将订单行树字段添加到自定义模块

Odoo 10 - Adding an Order Line Tree Field to Custom Module

我想添加一个带有树状视图的订单行字段,就像销售和维修模块中的那些一样。在这些模块中,表单底部有一个很好的部分,允许您在列表中输入产品和价格。我相信这些是由 sale.order.line 和 mrp.repair.line 模型处理的。

这是我正在谈论的图片: Sale Order Lines

我的目标是将类似的东西放入我自己的自定义模块中。我试过:

这是我到目前为止所做的。我已经注释掉了显示我提出解决方案的不同尝试的字段。最后两行显示我试图让价格和税费出现在我的自定义订单行中。

models.py

from odoo import models, fields, api
from odoo.addons import decimal_precision as dp

class mymodule_base(models.Model):
    _name = 'mymodule.mymodule'
    _description = 'Order'

    # sale_order_line = fields.One2many('sale.order.line', 'order_id', string='Order Lines',)

    # mrp_repair_line = fields.One2many('mrp.repair.line', 'repair_id', 'Operation Lines', copy=True, readonly=False,)

    # custom_order_line = fields.One2many('mymodule.line', 'order_id', 'Operation Lines')

class mymodule_line(models.Model): # My custom order lines model
    _name = 'mymodule.line'
    _description = 'Order Line'

    order_id = fields.Many2one('mymodule.mymodule', 'Order')

    product_id = fields.Many2one('product.product', 'Product', required=False)

    # tax_id = fields.Many2many('account.tax', 'repair_operation_line_tax','repair_operation_line_id', 'tax_id', 'Taxes')
                                    
    # price = fields.Many2many('product.product', 'list_price', 'Price')

views.xml

<odoo>
    <data>
        <!-- FORM VIEW -->
        <record id="mymodule.form" model="ir.ui.view">
            <field name="name">MyModule Form</field>
            <field name="model">mymodule.mymodule</field>
            <field name="arch" type="xml">
                <notebook>
                    <!-- Order Lines -->
                    <page string="Order Lines">
                        <field name="sale_order_line">
                            <tree string="Order Lines" editable="bottom">
                                <field name="product_id"/>
                            </tree>
                        </field>
                    </page>
                </notebook>
            </field>
        </record>
    </data>
</odoo>

我应该从这里去哪里?如果有任何帮助,我将不胜感激。

编辑 以下是我试图通过我的订单行实现的一些细节。在我的订单行中,我希望有以下字段:

与销售订单行非常相似,我希望根据产品名称 (product_id) 字段中的内容设置其他字段中的默认值。例如,根据产品 selected,价格和税金字段将自动填写以反映与该产品相关的价格和税金。这些字段还需要能够轻松修改,因为根据我的特殊需要,价格、税金和描述可能会根据情况发生变化。我已经尝试添加价格和税金字段,但我 运行 遇到了问题。我不知道如何正确设置这些字段的默认值以反映已与每个产品关联的内容。

首先,为不相关的模型重用现有模型(sale.order.linemrp.repair.line)不是一个好主意。每个模型都应该有一个特定的目的,并且只能用于该目的。您没有提到您的字段将用于什么,但通常我会建议使用您的 mymodule.line 示例。

澄清一下,您所指的字段类型称为 One2many field。它只是来自另一个模型的一组记录 (mymodule.line,它们链接到当前模型 (mymodule.mymodule) 的给定记录。

通常,当您在 One2many 字段中保存行时收到错误消息,这可能是因为未设置 required 字段(对于 mymodule.line 模型)。这是您在前两次尝试中出错的最可能原因。它通常很难调试,因为必填字段要么是 invisible,要么可能根本不在视图中。

I cannot figure out how to add any more fields than just product_id

您需要定义您想要在 mymodule.line 上拥有的所有字段。您要添加哪些字段,遇到什么麻烦?

我找到了解决办法。

在下面的示例中,我使用 onchange 方法根据所选产品中定义的字段使用适当的值和文本填充描述、价格和 tax_id 字段。每当更改 product_id 字段时,前面提到的字段都会根据新选择的产品自动更新新信息。

models.py

# -*- coding: utf-8 -*-

from odoo import models, fields, api
from odoo.addons import decimal_precision as dp

class mymodule_base(models.Model):
    _name = 'mymodule.mymodule' # model name
    _description = 'My Module'

    order_lines = fields.One2many('mymodule.line', 'line_id', 'Order Lines')

class mymodule_line(models.Model):
    _name = 'mymodule.line'
    _description = 'My Module Order Lines'

    line_id = fields.Many2one('mymodule.mymodule', string='Order') 

    product_id = fields.Many2one('product.template', string='Product') # Product name

    tax_id = fields.Many2many('account.tax', string='Taxes') # Product default taxes

    price = fields.Float(string='Price')

    description = fields.Text(string='Description')

    # Onchange method fills description, price, and tax_id fields based on product_id
    @api.onchange('product_id')
    def _onchange_product_id(self):
        self.description = self.product_id.description_sale # Fill description box from description_sale
        self.price = self.product_id.lst_price # Fill price box with default product price
        self.tax_id = self.product_id.taxes_id # Fill tax box with default tax (many2many)

views.xml

<record id="mymodule.form" model="ir.ui.view">
    <field name="name">My Module Form</field>
    <field name="model">mymodule.mymodule</field>
    <field name="arch" type="xml">
        <form>
            <sheet>
                <notebook>
                    <!-- Order Lines -->
                    <page string="Order Lines">
                        <field name="order_lines" mode="tree,kanban">
                            <tree string="Order Lines" editable="bottom">
                                <field name="product_id"/>
                                <field name="description"/>
                                <field name="price"/>
                                <field name="tax_id" widget="many2many_tags" domain="[('type_tax_use','=','sale')]"/>
                            </tree>
                        </field>
                    </page>
                </notebook>
            </sheet>
        </form>
    </field>
</record>

这对我来说效果很好,但如果有更好的方法来实现此功能,我愿意接受建议。感谢 travisw 为我指明了正确的方向!