This question has been flagged
2 Replies
6587 Views

Hi,

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.'),
]
Avatar
Discard
Best Answer

Hi,

Try this code

'activity_id' : fields.many2one("budget.activity_year", "ID", type="char", size=64, ondelete="no action"),

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':{}}
Avatar
Discard
Author

i tried..im getting this error : AttributeError: 'int' object has no attribute 'id'

I have update answer Pls check it.

object "budget.activity_year" and object "wpb.wpb" is different. Can you post definition of object "budget.activity_year" in here?

@ OpenERP Vietnam : Means ? I didn't get you

Author

@jack, the answer seems the same. what changes did you make?

names = self.pool.get('budget.activity_year').browse(cr, uid, activity_id, context=context) return {'value': {'activity_name': names.activity_name }} in here, you get column activity_name of object "budget.activity_year". Maybe, this object "budget.activity_year" have no column activity_name

@ Suthan: i have change your many2one field and also return {'value': {'activity_name': names.activity_name }} this line under if condition. This two changes i have done check it.

Author

@ OpenERP Vietnam, it has that column (activity_name), you can see it in my post.

ok got it and update answer

Author

jack, i can't change my many2one field to budget.activity_year, it has to be related to budget.activity_summary, that's the problem im having.

You have define ;budget.activity_year' this object and in this object there is field name 'activity_name' ? You try to browse data form 'budget.activity_year' this object but you dont have relation with this object.

You have define ;budget.activity_year' this object and in this object there is field name 'activity_name' ? You try to browse data form 'budget.activity_year' this object but you dont have relation with this object.

Author

I can change the relationship to budget.activity_year, it works, but i want the get activity_id from activity_summary object, because activity_summary object has other fields that i want to retrieve as well, only the activity_name, i want to get from activity_year. thats the issue. Is there a way to retrieve the activity name in budget.activity_year without changing the 'activity_id's many2one relation?

Add new many2one field relation with budget.activity_year

Author

@jack, yes..that works after adding new field. Thank you so much for all the help. appreciate it!

Ok then wht the answer

Best Answer

Try the below code:-

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

If activity_id in list then use for loop otherwise without for loop directly access table with browse method.

Avatar
Discard
Author

it doesn't work,i'm getting this error : NotImplementedError: Iteration is not allowed on browse_record(budget.activity_year, 16) @ prakash

@Suthan: In your code using budget.activity_summary table ID retrieve budget.activity_year table column value so logically wrong If you want budget.activity_year table column value then pass the budget.activity_year table ID

Author

@prakash, yes, i just learned about that. Thank you so much.