コンテンツへスキップ
メニュー
この質問にフラグが付けられました

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.

関連投稿 返信 ビュー 活動
0
7月 19
669
1
4月 23
13875
4
4月 23
27167
13
10月 17
18376
1
5月 25
3828