Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

onchange method not working

By
Sam path
on 5/5/14, 3:46 AM 3,280 views

Hi all,

I've created a masters for a terms and conditions. My-module.py

class tc_name(osv.osv): _name = 'tc.name'

def name_get(self, cr, uid, ids, context=None):
  res = []
  for code_obj in self.browse(cr, uid, ids, context=context):
      tc_name = code_obj.tc_name
      res.append((code_obj.id, tc_name))
  return res

_columns = {
            'tc_name': fields.char('Terms & Condition', size=150),
            'tc_desc': fields.text('Description')
            }

tc_name()

my_module.xml

<record id="view_tc_name_form" model="ir.ui.view">
    <field name="name">view.tc.name.form</field>
    <field name="model">tc.name</field>
    <field name="arch" type="xml">
        <form string="Terms and Conditions" version="7.0">
            <sheet>
                <group>
                    <group>
                        <field name="tc_name" strin="Terms and Condition" required="True"/>
                        <field name="tc_desc" required="True"/>
                    </group>
                </group>
            </sheet>
        </form>
    </field>
</record>

<record id="view_tc_name_tree" model="ir.ui.view">
    <field name="name">view.tc.name.tree</field>
    <field name="model">tc.name</field>
    <field name="arch" type="xml">
        <tree string="Terms and Conditions" version="7.0">
            <field name="tc_name" string="Terms and conditions"/>
        </tree>
    </field>
</record>

<record id="action_tc_name" model="ir.actions.act_window">
    <field name="name">Terms and Conditions</field>
    <field name="type">ir.actions.act_window</field>
    <field name="res_model">tc.name</field>
    <field name="view_type">form</field>
    <field name="view_mode">tree,form</field>
    <field name="view_id" ref="view_tc_name_tree"/>
</record>

<menuitem id="menu_tc_name_config" action="action_tc_name" name="Terms and Conditions" parent="base.menu_base_config" sequence="6"/>

this is working fine.

  • my_module.py

class extra_fields(osv.osv):

_inherit = "sale.order"
_columns = {
    'tc_name': fields.many2one('tc.name','Terms and Condition'),
    'tc_desc': fields.text('Description')
    }

def onchange_tc_name(self, cr, uid, ids, tc_name):
    v={}
    if tc_name:
        tc = self.pool.get('tc.name').browse(cr, uid, tc_name)
        v['tc_desc'] = tc.tc_desc
        return{'value':v}

my_module.xml

<record model="ir.ui.view" id="extra_fields_sale_order">
    <field name="name">sale.order.form</field>
    <field name="model">sale.order</field>
    <field name="inherit_id" ref="sale.view_order_form" />
    <field name="arch" type="xml">
        <group name='sale_total' position='after'>
            <group string="Terms and Conditions">
                <field name="tc_name" string="T & C" widget="selection" onchange="onchange_tc_name(tc_name, tc_desc, context)"/>
                <field name="tc_desc" string="Description"/>
            </group>
        </group>
    </field>
</record>

The master wosks fine and even it pull downs in sale order view but the onchange method does'nt work. I need the description of the terms and condition master to populate one the description field in sale order on change of terms and condition selected. Can anyone help me out?

Sorry i forgot to mention another thing can I edit the description field on sale order view after the onchange method executes

Sam path
on 5/5/14, 3:49 AM
3

René Schuster

--René Schuster--
1560
| 5 5 8
Weinheim, Germany
--René Schuster--

Challenge Everything!

René Schuster
On 5/5/14, 6:43 AM

In your .xml file use on_change="..." attribute instead of onchange="...".

Also, follow the advise of senthil and double check your function parameters.

Regards.

post scriptum: Yes, you can change the description field. But whenever you change tc_name the on_change method will be triggered and replace the description.

Thanks a lot Rene it worked for me..!

Sam path
on 5/5/14, 6:57 AM

Hi Rene, I have an another issue can you please help me out in that. The link is http://help.openerp.com/question/51091/filter-on-many2many-fields

Sam path
on 5/5/14, 7:00 AM
1

Jagdish Panchal

--Jagdish Panchal--
2674
| 5 3 6
Gandhinagar, India
--Jagdish Panchal--

OpenERP Developer

Jagdish Panchal
On 5/5/14, 4:23 AM

HI,

Try this code

def onchange_tc_name(self, cr, uid, ids, tc_name):
    v={}
    if tc_name:
        tc_name_id = self.pool.get('tc.name').search(cr, uid,[('tc_name','=',tc_name)])
        for tc_name_data in self.pool.get('tc.name').browse(cr, uid, c_name_id):
            v = { 'tc_desc': tc_name_data.tc_desc}

    return{'value':v}

i think both v['tc_desc'] = tc.tc_desc & v = { 'tc_desc': tc.tc_desc}

senthil
on 5/5/14, 5:02 AM

It doesn't work i tried both your answer but the data doesn't fetches.

Sam path
on 5/5/14, 5:47 AM

I have update answer pls try it.

Jagdish Panchal
on 5/5/14, 6:10 AM
1
senthil
On 5/5/14, 5:03 AM

I think parameter problem

def onchange_tc_name(self, cr, uid, ids, tc_name):

See you have only one paramter in the py file

and try to pass three parameters

<field name="tc_name" string="T & C" widget="selection" on_change="onchange_tc_name(tc_name, tc_desc, context)"/>

Change it like this and check

<field name="tc_name" string="T & C" widget="selection" on_change="onchange_tc_name(tc_name)"/>

If there is problem again check ur server log post it here

this doesn't work but there is no error displayed

Sam path
on 5/5/14, 6:08 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 5/5/14, 3:46 AM
Seen: 3280 times
Last updated: 3/16/15, 8:10 AM