Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
6 Răspunsuri
10282 Vizualizări

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):


Imagine profil
Abandonează
Cel mai bun răspuns

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

Imagine profil
Abandonează
Cel mai bun răspuns

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>


Imagine profil
Abandonează
Autor Cel mai bun răspuns

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')
Imagine profil
Abandonează

Hi Tony,

Glad to know it helped!

Regards,

Mayank Gosai

Cel mai bun răspuns

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

No need any function.

Imagine profil
Abandonează

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.

Related Posts Răspunsuri Vizualizări Activitate
0
iul. 19
669
1
apr. 23
13381
4
apr. 23
26660
13
oct. 17
18023
1
mai 25
3435