Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie

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


Awatar
Odrzuć
Najlepsza odpowiedź

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

Awatar
Odrzuć
Najlepsza odpowiedź

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>


Awatar
Odrzuć
Autor Najlepsza odpowiedź

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')
Awatar
Odrzuć

Hi Tony,

Glad to know it helped!

Regards,

Mayank Gosai

Najlepsza odpowiedź

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

No need any function.

Awatar
Odrzuć

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.

Powiązane posty Odpowiedzi Widoki Czynność
0
lip 19
669
1
kwi 23
13309
4
kwi 23
26499
13
paź 17
17958
1
maj 25
3346