Search code examples
odooodoo-8odoo-9

2 fields in tree view


In the product category, I added a product_ids field and I want to display it with a tree view showing name and default_code of products. For some reason I get the error "Field default_code does not exist"

<record id="view_product_category_qty_discount" model="ir.ui.view">
    <field name="name">product.category.inherit.qty.discount.Config Hetlita</field>
    <field name="model">product.category</field>
    <field name="type">form</field>
    <field name="inherit_id" ref="product.product_category_form_view" />
    <field name="arch" type="xml">
        <form position="inside">
            <group col="2" colspan="2">
                <separator string="Quantity for discount" colspan="2"/>
                <field name="qty_for_discount" />
            </group>
            <group>
                <field name="product_ids" widget="many2many_tags"/>
                <tree>
                    <field name="name"/>
                    <field name="default_code"/>
                </tree>
            </group>
        </form>
    </field>
</record>
class ProductCategory(models.Model):
    _inherit = 'product.category'

    qty_for_discount = fields.Float(string='Qty For Discount')
    product_ids = fields.Many2many(
        'product.template', string='Products')

Solution

  • That's because there is no default_code on model product.template but instead on its variants with model product.product. I would change the field on product.category to a One2Many on product.product:

        product_ids = fields.One2many(
            comodel_name='product.product',
            inverse_name='categ_id',
            string='Products')
    

    And there is a mistake in your xml:

                        <group>
                            <field name="product_ids">
                                <tree>
                                    <field name="name"/>
                                    <field name="default_code"/>
                                </tree>
                            </field>
                        </group>