Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
6 Відповіді
10667 Переглядів

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


Аватар
Відмінити
Найкраща відповідь

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

Аватар
Відмінити
Найкраща відповідь

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>


Аватар
Відмінити
Автор Найкраща відповідь

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')
Аватар
Відмінити

Hi Tony,

Glad to know it helped!

Regards,

Mayank Gosai

Найкраща відповідь

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

No need any function.

Аватар
Відмінити

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 Відповіді Переглядів Дія
0
лип. 19
669
1
квіт. 23
13876
4
квіт. 23
27170
13
жовт. 17
18376
1
трав. 25
3830