This question has been flagged

What I aim to do is to add a new field called category in the sale order line , and when I choose the product the corresponding category (ie, consumable/service/stockable) should come automatically to the field. I have successfully added the field to the order line. What to write in the function to get the product category in the field?

here is my code

.xml

<odoo> 
 <data>
     <record id="product_category_sale_line" model="ir.ui.view">
          <field name="name">sale.orderline.inherited</field>
         <field name="model">sale.order</field>
         <field name="inherit_id" ref="sale.view_order_form"/>
         <field name="arch" type="xml">
         <xpath expr="//field[@name='order_line']/tree/field[@name='price_unit']" position="after">
             <field name="category"/>
         </xpath>
         </field>
     </record>
 </data>
</odoo>

.py

from odoo import models, fields, api
class ProductCategory(models.Model):
 _inherit = 'sale.order'
 category = fields.Char(string='Category', compute='get_category')

 def get_category(self):


Avatar
Discard
Best Answer

Hi Tony,

Use Related Field instead of making computing.

Try this:

in your .py file:

        product_categ_id = fields.Many2one(related = 'product_id.product_tmpl_id.categ_id', string='Product Category')

In your .xml file:

    

<odoo>

<data>

<record id="product_category_sale_line" model="ir.ui.view">

<field name="name">sale.orderline.inherited</field>

<field name="model">sale.order</field>

<field name="inherit_id" ref="sale.view_order_form"/>

<field name="arch" type="xml">

<xpath expr="//field[@name='invoice_line_ids']/tree/field[@name='product_id']" position="after">

<field name="product_categ_id"/>

</xpath>

</field>

</record>

</data>

</odoo>


Hope it Helps,

Regards,

Mayank Gosai

Avatar
Discard
Best Answer

1) update __init__.py

2) update mainifest with xml and add 'purchase' in its data like 'data': ['purchase' ]

PY File:
from odoo import models, fields, api

            class ProductCategory(models.Model):

 _inherit= 'purchase.order.line'

product_categ_id = fields.Selection(string='Product Type',related='product_id.type')


XML File:

<odoo>

<data>

  <record id="purchase_order_line_form" model="ir.ui.view">

       <field name="name">purchase order line form</field>

       <field name="model">purchase.order</field>

       <field name="inherit_id" ref="purchase.purchase_order_form"/>

       <field name="arch" type="xml">

           <xpath expr="//form/sheet/notebook/page/field/tree/field[@name='product_id']" position="after">

               <field name="product_categ_id"/></xpath>

</field>

   </record>

  </data>

</odoo>


Avatar
Discard
Author Best Answer

Thank you Mayank Gosai, for your reply. It really helped me to get the problem solved. Although what I wanted was to get the product type(ie, stockable,consumable,service)(Sorry, I mis-typed it to category and made the question ambiguous). I updated your code with some modifications to select the product type.
In the .py file



_inherit = 'sale.order.line'
product_categ_id = fields.Selection(related='product_id.product_tmpl_id.type', string='Category')
Avatar
Discard

Hi Tony,

Glad to know it helped!

Regards,

Mayank Gosai

Best Answer

you can take one related field in sales order line object which belongs to product_id.category_id.

No need any function.

Avatar
Discard

He want to show the product type in sales order line and he want the added field to be filled automatically from product.template once he added the product.