我在创建的视图中添加了一个按钮,但出现此错误:
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