Skip to Content
Menu
This question has been flagged
3 Replies
2795 Views

A try to modified purchase_order_line but onchange_product and field related are wrong .

Here my new class product_template :

class product_template(osv.osv):
    _name = 'product.template'
    _inherit = 'product.template'

_columns = { 'my_field_id': fields.many2one('product.uom','My_Field'),
the new .xml for product_template and purchase_order_line views :
   <record id="product_template_form_view" model="ir.ui.view">
        <field name="name">product.template.common.form</field>
        <field name="model">product.template</field>
        <field name="inherit_id" ref="product.product_template_form_view" />
        <field eval="25" name="priority"/>
        <field name="arch" type="xml">
            <field name="uom_id" position="after">
                <field name="my_field_id"/>
            </field>
		</field>
    </record>

    <record id="purchase_order_form" model="ir.ui.view">
        <field name="name">purchase_order_form</field>
        <field name="model">purchase.order</field>
        <field name="inherit_id" ref="purchase.purchase_order_form" />
		<field eval="90" name="priority"/>
        <field name="arch" type="xml">
			<xpath expr="/form[@string='Purchase Order']/sheet/notebook/page[@string='Products']/field[@name='order_line']/tree[@string='Purchase Order Lines']/field[@name='price_unit']" position="before">
                <field name="my_field_po" string="My_Field"/>
			</xpath>
 </field> 
    </record>	
And class purchase_order_line for which I ask little help
class purchase_order_line(osv.osv):
    _name = 'purchase.order.line'
    _inherit = 'purchase.order.line'

def onchange_product_id(self, cr, uid, ids, pricelist_id, product_id, qty, uom_id, partner_id, date_order=False, fiscal_position_id=False, date_planned=False, name=False, price_unit=False, state='draft', context=None): res = super(purchase_order_line, self).onchange_product_id(cr, uid, ids, pricelist_id, product_id, qty, uom_id, partner_id, date_order=date_order, fiscal_position_id=fiscal_position_id, date_planned=date_planned, name=name, price_unit=price_unit, state=state, context=context) if 'value' in res: my_field_po = self.pool.get('product.product').browse(cr, uid, product_id, context=context).unitestock_po_id res['value'].update({'my_field_po': my_field_po and my_field_po.id or False}) return res _columns = { 'my_field_po': fields.related('product_id','my_field_id',type='many2one',relation='product.template',string='My_Field',store=True), }
Avatar
Discard

Your onchange_product_id is executed?

Author

I think no, the field in the p.o.line is always "white" when I choose a product . This is my question.

Best Answer

1. You verify 'depends': [ .... ]   in __openerp__py  in your module

2. Verify indents in your .py  code  --- in your example ...class... and ...def... has the same!?!?

3. You enable debug log file and, for example, add before return:

import logging
_logger = logging.getLogger(__name__)
_logger.info("FOO RETURN %s %s", 'RES', res) 

If your code is executed, in log file you need to find string "FOO RETURN"

UPDATE

4. Defined field = my_field_id  but in related you get  my_field__id   (duplicated  char "_" )


UPDATE

Code bellow works without problems in my test system. Note the bold line and comment on it. What is unitestock_po_id?

class product_template(osv.osv):
_name = 'product.template'
_inherit = 'product.template'

_columns = {
'my_field_id': fields.many2one('product.uom','My_Field'),
}

class purchase_order_line(osv.osv):
_name = 'purchase.order.line'
_inherit = 'purchase.order.line'

def onchange_product_id(self, cr, uid, ids, pricelist_id, product_id, qty, uom_id,
partner_id, date_order=False, fiscal_position_id=False, date_planned=False,
name=False, price_unit=False, state='draft', context=None):
res = super(purchase_order_line, self).onchange_product_id(cr, uid, ids, pricelist_id, product_id, qty, uom_id,
partner_id, date_order=date_order, fiscal_position_id=fiscal_position_id, date_planned=date_planned,
name=name, price_unit=price_unit, state=state, context=context)
if 'value' in res:
my_field_po = self.pool.get('product.product').browse(cr, uid, product_id, context=context) ###.unitestock_po_id
res['value'].update({'my_field_po': my_field_po and my_field_po.id or False})
return res

_columns = {
'my_field_po': fields.related('product_id','my_field_id',type='many2one',relation='product.template',string='My_Field',store=True),
}

UPDATE

    def onchange_product_id(self, cr, uid, ids, pricelist_id, product_id, qty, uom_id,
partner_id, date_order=False, fiscal_position_id=False, date_planned=False,
name=False, price_unit=False, state='draft', context=None):
res = super(purchase_order_line, self).onchange_product_id(cr, uid, ids, pricelist_id, product_id, qty, uom_id,
partner_id, date_order=date_order, fiscal_position_id=fiscal_position_id, date_planned=date_planned,
name=name, price_unit=price_unit, state=state, context=context)
if 'value' in res:
my_field_po = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
res['value'].update({'my_field_po': my_field_po and my_field_po.product_tmpl_id.my_field_id or False})
return res

_columns = {
'my_field_po': fields.related('product_id','my_field_id',type='many2one',relation='product.uom',string='My_Field',store=True),
}

Avatar
Discard
Author

Point 1 was OK but you're right for indents. Thank you. But now : AttributeError: 'product.product' object has no attribute 'my_field_id' If I change 'product.product' to product.template' in onchange_product AttributeError: 'product.template' object has no attribute 'my_field_id' Yet 'my_field_id' is in my class product_template.

Answer updated

Author

No excuses me, double '_' due to an error in my question. In fact there are many that only underscore. i edit my question.

Author

I have same error : AttributeError: 'product.template' object has no attribute 'my_field_id'

Answer updated

Author

I test with your code . I have no error. But in the tree of PO.line the value of "my_field_po" is "product.id" and I want a value like 'product.uom" who is inherited with class product_template. What is unitestock_po_id? just an error ( I fac in my codes the field "my_field" is "unitestock_po" ) I change my codes before posting in the forum to read simplier for you !

For "product.uom" relation should be defined as relation='product.uom', see updated answer with new code.

Author

I test with 'relation='product.uom' >> Internal Server Error I dont understand. The relation is not with 'product.uom' The relation is with a field ( 'my_field_id' ) wich is already in relation M2O with 'product.uom'. class uom class product.template class purchase.order0.line uom.id ....................> my_field_id..(M2O)..................................>my_field_po (related)

Other changes in my code not only product.uom., for exapmple my_field_po.product_tmpl_id.my_field_id. My last code works without errors.

Author

Ok this code works without errors ! Now I try to make the same with another field .