Question

I have a class with an onchange method which retrieves the activity name from another model. when I run it, im getting an error like this.

AttributeError: 'Field activity_name not found in browse_record(budget.activity_year, 16)'

Can anyone tell why this is happening and how may i fix it?

My class with onchange method

class wpb(osv.osv):
_name = "wpb.wpb"
_description = "Work Program and Budget"
_columns = {
    'activity_id' : fields.many2one("budget.activity_summary", "ID", type="char", size=64, ondelete="no action"),
    'activity_name' : fields.char("Activity Name", type="char", size=64, ondelete="no action" ),
    'region_id' : fields.related("activity_code", "region_id", type="char", string="Management Unit"),
    'service_lvl' : fields.float("Service Level", digits=(4,2)),
    'annual_work' : fields.float("Annual Work", digits=(4,2)),
       }

def onchange_activity_code(self, cr, uid, ids, activity_id, context = None):
    if activity_id:
         names = self.pool.get('budget.activity_year').browse(cr, uid, activity_id, context=context)
    return {'value': {'activity_name': names.activity_name }}
    return {'value':{}}

The xml view

 <record id="wpb_form" model="ir.ui.view">
        <field name="name">wpb.wpb.form</field>
        <field name="model">wpb.wpb</field>
        <field name="arch" type="xml">
            <form string="Work Program and Budget" version="7.0">
                <group col="2">
                    <field name="activity_id" on_change="onchange_activity_code(activity_id)"/>
                    <field name="activity_name"/>
                    <field name="region_id" readonly="1"/>
                    <field name="service_lvl"/>
                    <field name="annual_work"/>
                </group>
            </form>
        </field>
    </record>


<record id="wpb_view" model="ir.ui.view">
        <field name="name">wpb.wpb.view</field>
        <field name="model">wpb.wpb</field>
        <field name="type">tree</field>
        <field name="arch" type="xml">
            <tree string="Work Program and Budget" editable="top">
                <field name="activity_id" on_change="onchange_activity_code(activity_id)"/>
                <field name="activity_name"/>
                <field name="region_id" readonly="1"/>
                <field name="annual_work"/>
            </tree>
        </field>
    </record>

The class im trying to get the activity name field

class activity_year(osv.osv):
_name = "budget.activity_year"
_description = "Activity year"
_rec_name = "activity_yearcode"
_columns = {
    'activity_yearcode' : fields.char("Activity Code", size=64, required=True),
    'activity_name' : fields.char("Activity Name", size=128),
    'act_status' : fields.selection([
                ('1', 'All'),
                ('2', 'Active'),
                ('3', 'Inactive'),
                ], 'Status'),
    }
_sql_constraints = [
    ('activity_yearcode_unique', 'UNIQUE(activity_yearcode)', 'Each activity code is unique.'),
]
Was it helpful?

Solution

Your activity_id is relationship with budget.activity_summary and you get value from budget.activity_year. I think that might be relationship with budget.activity_year. Change this relationship and try below on_change method.

try this on_change

def onchange_activity_code(self, cr, uid, ids, activity_id, context = None):
    res = {}
    names = self.pool.get('budget.activity_summary').browse(cr, uid, activity_id, context=context)
    if names:
        res['activity_name'] = names.activity_name
    return {'value': res}

And please see other on_change format so you will get more clear idea about it.

On Change Method Tips

Hope this will help you.

OTHER TIPS

Update your onchange method

def onchange_activity_code(self, cr, uid, ids, activity_id, context = None):
    if activity_id:
         names = self.pool.get('budget.activity_year').browse(cr, uid, [activity_id], context=context)
         return {'value': {'activity_name': names.activity_name }}

.py file

from openerp.osv import fields, osv

class activity_year(osv.osv):
    _name = "budget.activity_year"
    _description = "Activity year"
    _rec_name = "activity_yearcode"
    _columns = {
        'activity_yearcode' : fields.char("Activity Code", size=64, required=True),
        'activity_name' : fields.char("Activity Name", size=128),
        'act_status' : fields.selection([
                    ('1', 'All'),
                    ('2', 'Active'),
                    ('3', 'Inactive'),
                    ], 'Status'),
        }
    _sql_constraints = [
        ('activity_yearcode_unique', 'UNIQUE(activity_yearcode)', 'Each activity code is unique.')
     ]

class wpb(osv.osv):
    _name = "wpb.wpb"
    _description = "Work Program and Budget"
    _columns = {
        'activity_id' : fields.many2one("budget.activity_summary", "ID", type="char", size=64, ondelete="no action"),
        'activity_name' : fields.char("Activity Name", type="char", size=64, ondelete="no action" ),
        'region_id' : fields.related("activity_code", "region_id", type="char", string="Management Unit"),
        'service_lvl' : fields.float("Service Level", digits=(4,2)),
        'annual_work' : fields.float("Annual Work", digits=(4,2)),
    }

    def onchange_activity_code(self, cr, uid, ids, activity_id, context = None):
        if activity_id:
             names = self.pool.get('budget.activity_year').browse(cr, uid, [activity_id], context=context)
             return {'value': {'activity_name': names.activity_name }}

activity_name field set value from budget.activity_year object.

The type of name is list of records, so for browse the list of records, you must define the element. In your case, you can type : name[0].activity_name --> for the activity_name of the first record of name. Forgive me for my bad english

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top