Odoo:如何显示在一个多对多字段中的many2one字段的字段

时间:2019-12-21 20:55:03

标签: odoo odoo-12

我不知道如何放置它,但这就是我想要的,我想在一个one2many字段的树形视图中显示custom.product模型的字段,我的代码如下

class CustomSale(models.Model):
    _name = 'custom.sale'
    _description = 'Sale Record'

    name = fields.Char(string='Order Reference', required=True, copy=False, readonly=True,
                       default=lambda self: _('New'))
    order_line = fields.One2many('custom.sale.line', 'order_id', string='Order Lines', copy=True,
                                 auto_join=True)


class CustomSaleLine(models.Model):
    _name = 'custom.sale.line'
    _description = 'Sales Line'

    order_id = fields.Many2one('custom.sale', string='Order Reference', required=True,)

    product_id = fields.Many2one('custom.product', string='Product', change_default=True, ondelete='restrict')
    product_uom_qty = fields.Integer(string='Ordered Quantity', required=True, )
<record id="form_custom_sale" model="ir.ui.view">
        <field name="name">custom.sale.form</field>
        <field name="model">custom.sale</field>
        <field name="arch" type="xml">
            <form string="Sales">
                <sheet>
                    <group>
                        <group>
                            <field name="name"/>
                        </group>
                    </group>
                    <notebook>
                        <page string="Order Lines" name="order_lines">
                            <field name="order_line" widget="section_and_note_one2many" mode="tree">
                                <tree editable="bottom">
                                    <control>
                                        <create string="Add a product"/>
                                    </control>
                                    <field name="product_id">
                                        <tree>
                                            <field name="brand_id"/>
                                            <field name="country_id"/>
                                            <field name="sell_price"/>
                                        </tree>
                                    </field>
                                    <field name="product_uom_qty" string="Ordered Qty"/>
                                </tree>
                            </field>
                        </page>
                    </notebook>
                </sheet>
            </form>
        </field>
    </record>

但是我仍然无法显示“ brand_id”,“ country_id”和“ sell_price”

enter image description here

1 个答案:

答案 0 :(得分:1)

为要在树状视图中显示的字段添加related字段。

class CustomSaleLine(models.Model):
    _name = 'custom.sale.line'
    _description = 'Sales Line'

    order_id = fields.Many2one('custom.sale', string='Order Reference', required=True,)

    product_id = fields.Many2one('custom.product', string='Product', change_default=True, ondelete='restrict')
    product_uom_qty = fields.Integer(string='Ordered Quantity', required=True, )
    brand_id = fields.Many2one('BRAND_MODEL_HERE',related='product_id.brand_id')
    country_id = fields.Many2one('COUNTRY_MODEL_HERE',related='product_id.country_id')
    sell_price = fields.Float(related='product_id.sell_price')


<record id="form_custom_sale" model="ir.ui.view">
    <field name="name">custom.sale.form</field>
    <field name="model">custom.sale</field>
    <field name="arch" type="xml">
        <form string="Sales">
            <sheet>
                <group>
                    <group>
                        <field name="name"/>
                    </group>
                </group>
                <notebook>
                    <page string="Order Lines" name="order_lines">
                        <field name="order_line" widget="section_and_note_one2many" mode="tree">
                            <tree editable="bottom">
                                <control>
                                    <create string="Add a product"/>
                                </control>
                                <field name="product_id">
                                <field name="brand_id"/>
                                <field name="country_id"/>
                                <field name="sell_price"/>
                                <field name="product_uom_qty" string="Ordered Qty"/>
                            </tree>
                        </field>
                    </page>
                </notebook>
            </sheet>
        </form>
    </field>
</record>