在视图OpenERP 7中添加按钮时出错

时间:2014-06-25 08:57:30

标签: python xml module openerp odoo

我在视图中添加了一个按钮,但我收到了这个错误:

AttributeError: 'student_student' object has no attribute 'set_age'

这是student_info.py文件:

from osv import osv, fields


class student_student(osv.osv):
    _name = 'student.student'
    _inherit = 'student.student' #in another question on stackoverflow they said that writing this solved the same error, but it hasn't worked for me
    _columns = {
            'name' : fields.char('Student Name', size = 16, required = True, translate = True),
            'age' : fields.integer('Age'),
            'percentage' : fields.float('Percentage', help = 'This field will add average marks of the students out of 100'),
            'gender' : fields.selection([('male', 'Male'), ('female', 'Female')], 'Gender'),
            'active' : fields.boolean('Active'),
            'notes' : fields.text('Details'),
            }
    _defaults = {
            'name' : 'Atul',
            'active' : True,
            'age' : 13,
            }
    def set_age(self, cr, uid, ids, context=None):
        for prod in self.browse(cr, uid, ids, context=context):
            my_age = 33 
        self.write(cr, uid, prod.id, {'age': my_age })

student_student()

这是student_info_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>


<!--Student search view-->
<record model="ir.ui.view" id="student_search" >
    <field name="name">student.search</field>
    <field name="model">student.student</field>
    <field name="type">search</field>
    <field name="arch" type="xml">
        <search string = "student information search">
     <field name="name" string = "Student Name" />
         <field name="gender" string = "Gender" />  
         <field name="age" string = "Age" />
    </search>
    </field>
</record>


<!--Student tree View-->
<record id="student_student_tree" model="ir.ui.view">
<field name="name">student.result.tree</field>
   <field name="model">student.student</field>
   <field name="type">tree</field>
   <field name="arch" type="xml">
     <tree string="Student_result">  
       <field name="name" />  
       <field name="age" />
       <field name="percentage"/> 
       <field name="gender"/>  
       <field name="active"/>
       </tree>
   </field>
 </record>


<!--Student Form View-->
<record id="student_student_form" model="ir.ui.view">
<field name="name">student.result.form</field>
<field name="model">student.student</field>
<field name="type">form</field>
     <field name="arch" type="xml">
     <form string="Student_result">  
       <field name="name" />  
       <field name="age" />
       <field name="percentage"/> 
       <field name="gender"/>  
       <field name="active"/>
       <field name="notes"/>
       <button name="set_age" string="MY test function" type="object" />
     </form>
     </field>
</record>

<!--Student Action-->
<record id="action_student_student" model="ir.actions.act_window">
  <field name='name'>Student Information</field>
  <field name='res_model'>student.student</field>
  <field name='view_type'>form</field>
  <field name='view_mode'>tree,form</field>
</record>


<!--Student Menu-->
<menuitem id="student_parent" name="Student"/>
<menuitem id="menu_student_parent" name="Student Management" parent="student_parent"></menuitem>
<menuitem action="action_student_student" id="menu_student_student" parent="menu_student_parent" string="Result"/>

</data>
</openerp>

这个set_age()方法应该为所有学生设置学生的年龄为33岁

1 个答案:

答案 0 :(得分:0)

你的函数是不可调用的,这就是为什么这个错误,检查你的函数缩进,这里你的代码工作正常。重新启动服务器并升级您的模块,它将工作。