onchange方法中的属性错误

时间:2014-04-23 03:45:48

标签: python openerp onchange openerp-7

我有一个带有onchange方法的类,它从另一个模型中检索活动名称。当我运行它时,我得到这样的错误。

AttributeError:'在browse_record中找不到字段activity_name(budget.activity_year,16)'

任何人都可以说出为什么会这样,我该如何解决?

使用onchange方法的课程

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

xml视图

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

我试图获取活动名称字段的类

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.'),
]

3 个答案:

答案 0 :(得分:1)

您的activity_idbudget.activity_summary有关系,您可以从budget.activity_year获得价值。我认为这可能与budget.activity_year有关系。更改此关系并尝试使用on_change方法。

试试这个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}

请参阅其他on_change格式,以便您更清楚地了解它。

On Change Method Tips

希望这会对你有所帮助。

答案 1 :(得分:1)

更新 onchange 方法

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文件

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 }}
来自budget.activity_year对象的

activity_name字段设置值。

答案 2 :(得分:0)

名称的类型是记录列表,因此要浏览记录列表,必须定义元素。 在您的情况下,您可以键入:name [0] .activity_name - &gt;对于名称的第一个记录的activity_name。 原谅我的英语不好