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

时间:2017-08-19 10:34:17

标签: python openerp odoo-10

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

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

我的目标是将这样的东西放到我自己的自定义模块中。我试过了:

  • 使用sale.order.line添加One2many字段( sale_order_line = fields.One2many(' sale.order.line',' order_id',string =&#39 ;订单行',))。通过正确的视图,这条线让我几乎得到了我想要的东西。但是,当我尝试保存记录时,它会给我一个"记录不存在或已被删除"错误。
  • 使用mrp.repair.line添加One2many字段( mrp_repair_line = fields.One2many(' mrp.repair.line',' repair_id','操作Lines',copy = True,readonly = False,))。再次,这几乎可以工作,但它说"您必须在修复表单中选择一个价目表! 请在选择产品前设置一个"当我试图保存记录时。
  • 关注this method以使销售订单行与我的模块一起使用。我尝试保存记录时收到验证错误。它给了我一个关于错误的提示(参考对象:产品计量单位 - product.uom),但我无法解决这个问题。
  • 根据sales和mrp_repair模块中的代码创建自己的订单行模型及其自己的字段和One2many / Many2one关系。这实际上允许我保存记录,但我无法弄清楚如何添加除了product_id之外的任何其他字段( product_id = fields.Many2one(' product.product',' Product&# 39;,required = False))。

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

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)字段中的内容设置其他字段中的默认值。例如,根据所选产品,将自动填写价格和税金字段,以反映与该产品相关的价格和税收。这些字段也需要能够轻松修改,因为根据具体情况,价格,税收和描述可能会根据情况而变化。我已经尝试添加价格和税收字段,但我遇到了问题。我无法弄清楚如何正确地默认这些字段以反映每个产品已经关联的内容。

2 个答案:

答案 0 :(得分:2)

首先,将现有模型(sale.order.linemrp.repair.line)重新用于不相关的模型是一个坏主意。每个模型都应具有特定目的,仅用于此目的。您尚未提及您的字段将用于哪些内容,但通常我会建议您使用mymodule.line示例。

为了澄清,您所指的字段类型称为One2many field。它只是来自另一个模型(mymodule.line的一组记录,它们与当前模型(mymodule.mymodule)中的给定记录相关联。

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

  

我无法弄清楚如何添加除product_id

之外的其他字段

您需要定义mymodule.line上要包含的所有字段。您要添加哪些字段以及您遇到了哪些麻烦?

答案 1 :(得分:0)

我找到了解决方案。

在下面的示例中,我使用onchange方法根据所选产品中定义的字段,使用适当的值和文本填充description,price和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指出我正确的方向!