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

江枫思渺然 提交于 2019-12-06 03:33:15

Firstly, reusing an existing model (sale.order.line or mrp.repair.line) for an unrelated model is a bad idea. Each model should have a specific purpose and only be used for that purpose. You haven't mentioned what your field will be used for, but typically I would suggest using your mymodule.line example.

Just to clarify, the type of field you're referring to is called a One2many field. It's just a group of records from another model (mymodule.line that are linked to a given record from the current model (mymodule.mymodule).

Typically, when you receive errors on saving a line on the One2many field, it's probably because a required field (for the mymodule.line model) is not set. This is the most likely reason you were getting errors with the first two attempts. It's typically hard to debug because the required field is either invisible or possibly not on the view at all.

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

You'll need to define all of the fields you want to have on your mymodule.line. What fields are you trying to add and what trouble are you having?

I have found a solution.

In the following example, I use an onchange method to fill the description, price, and tax_id fields with the appropriate values and text based on the fields defined in the product being selected. Whenever the product_id field is changed, the previously mentioned fields automatically update with new information based on the newly selected product.

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>

This works quite well for me, but if there is a better way of implementing this feature, I am open to suggestions. Thanks to travisw for pointing me in the right direction!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!