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

Attribute error in onchange method

By
Suthan
on 4/23/14, 1:49 AM 1,893 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.'),
]
0

Jagdish Panchal

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

OpenERP Developer

Jagdish Panchal
On 4/23/14, 2:33 AM

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

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

Suthan
on 4/23/14, 2:45 AM

I have update answer Pls check it.

Jagdish Panchal
on 4/23/14, 3:20 AM

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

OpenERP Vietnam
on 4/23/14, 3:53 AM

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

Jagdish Panchal
on 4/23/14, 4:02 AM

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

Suthan
on 4/23/14, 4:51 AM

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

OpenERP Vietnam
on 4/23/14, 4:51 AM

@ 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.

Jagdish Panchal
on 4/23/14, 5:03 AM

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

Suthan
on 4/23/14, 5:06 AM

ok got it and update answer

Jagdish Panchal
on 4/23/14, 5:13 AM

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.

Suthan
on 4/23/14, 5:16 AM

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.

Jagdish Panchal
on 4/23/14, 5:32 AM

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.

Jagdish Panchal
on 4/23/14, 6:28 AM

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?

Suthan
on 4/23/14, 7:19 AM

Add new many2one field relation with budget.activity_year

Jagdish Panchal
on 4/23/14, 7:38 AM

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

Suthan
on 4/23/14, 8:28 AM

Ok then wht the answer

Jagdish Panchal
on 4/23/14, 8:33 AM
0

Prakash

--Prakash--
3173
| 5 4 7
Bikaner, India
--Prakash--

OpenERP Devloper at SunARC Technology

Prakash
On 4/23/14, 3:14 AM

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.

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

Suthan
on 4/23/14, 4:40 AM

@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

Prakash
on 4/23/14, 7:23 AM

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

Suthan
on 4/23/14, 8:26 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: 4/23/14, 1:49 AM
Seen: 1893 times
Last updated: 3/16/15, 8:10 AM