Openerp 7:AttributeError:" Field' name'对象' browse_record(wizard_set_ages,1)'"中不存在

时间:2014-06-26 10:14:40

标签: python openerp wizard openerp-7 odoo

我有一个向导需要从模块中获取一些字段,但是当我尝试执行此操作时出现此错误:

AttributeError: "Field 'name' does not exist in object 'browse_record(wizard_set_ages, 1)'"

问题是我不知道如何从字段中获取数据。我需要迭代所有选定的记录并执行该操作(将名称写入大写锁定中的描述中,如名称:Atul - > set_description() - > description:ATUL) 这是wizard.py代码:

from osv import osv, fields

class test_wizard(osv.osv_memory):
    _name='wizard_set_ages'
    _columns={}

    def set_all_age(self, cr, uid, ids, context=None):
        mystudents = self.pool.get('student.student')
    for student in mystudents.browse(cr, uid, ids, context=context):
        my_description = str(student.name).upper()
        mystudents.write(cr, uid, student.id, {'notes' : my_description})

"""edit same for set_all_age        def set_selected_age(self, cr, uid, ids, context=None):
    for prod in self.browse(cr, uid, ids, context=context):         
            my_details = str(prod.name).upper()
        self.write(cr, uid, prod.id, {'notes': my_details }) """

,这是student.py文件:

from osv import osv, fields


class student_student(osv.osv):
    _name = 'student.student'
    _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):
            dict = self.read(cr, uid, ids, ['name'])            
                my_details = str(prod.name).upper()
            self.write(cr, uid, prod.id, {'notes': my_details })
        return True

student_student()

<小时/> 编辑:添加了wizard_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>        
    <!-- create a view with a unique id -->
        <record id="view_set_all_ages_wizard" model="ir.ui.view">
            <field name="name">wizard_set_ages_form</field>
            <field name="model">wizard_set_ages</field>
            <field name="type">form</field>
            <field name="arch" type="xml">
            <!-- create a normal form view, with the fields you've created on your python file -->
                <form string="Set All Student Ages" version="7.0">
                    <group >
                        <separator string="TEST" colspan="2"/>
                        <newline/>
                    </group>
                    <div style="text-align:right">
                        <button icon="gtk-cancel" special="cancel" string="Cancel"/>
                        <button icon="gtk-ok" name="set_all_age" string="Set All Details" type="object" />
            <button icon="gtk-ok" name="set_selected_age" string="Set Only Selected Details" type="object" />
                    </div>

               </form>
            </field>
        </record>
        <!-- your action window refers to the view_id you've just created -->
        <record id="action_set_all_ages" model="ir.actions.act_window">
            <field name="name">Set All Ages</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">wizard_set_ages</field>
            <field name="view_type">form</field>
            <field name="view_mode">form</field>
            <field name="view_id" ref="view_set_all_ages_wizard"/>
            <field name="target">new</field> 
        </record>

        <act_window id="action_set_all_ages"
                name="Set All Ages"
                res_model="wizard_set_ages" 
        src_model="student.student"
                view_mode="form" 
                target="new"
        key2="client_action_multi"
        />
    </data>
</openerp>

3 个答案:

答案 0 :(得分:0)

wizard_set_ages中没有字段,但您尝试了

for prod in self.browse(cr, uid, ids, context=context):     
            my_details = str(prod.name).upper()

使用myobj代替(myobj = self.pool.get('student.student'))更新并读取该表

答案 1 :(得分:0)

回到你的第一个问题&#34;浏览记录向导中没有名称&#34;,只有两件事触发了这个,模型没有一个名为name的列或者......你浏览了一条不存在的记录。

然而,在你的第二个问题..

您的按钮位于具有wizard_set_ages支持模型的表单上,这意味着当用户单击它时,它会在wizard_set_ages模型上执行set all age。然后,您在student.student上执行self.pool.get并使用ids参数进行浏览,但这是您的问题。由于表单在向导模型上,因此ids参数包含向导模型的ID,而不是学生模型,因此您无法使用它们来浏览学生模型。通常的模式是搜索学生ID或在创建时将其存储在向导中。看看选股向导的一个例子。

答案 2 :(得分:0)

尝试使用此代码即可。

def set_all_age(self, cr, uid, ids, context=None):
        mystudents = self.pool.get('student.student')
        searching_val= mystudents.search(cr, uid, [('id', '!=', ids)])
        for student in mystudents.browse(cr, uid, searching_val, context=context):
            my_description = str(student.name).upper()
            mystudents.write(cr, uid, student.id, {'notes': my_description})

    def set_selected_age(self, cr, uid, ids, context=None):
        mystudents = self.pool.get('student.student')
        data = self.read(cr, uid, ids,)[-1]
        ss = context.get('active_ids') and context.get('active_ids')[0] or False,
        searching_val= mystudents.search(cr, uid, [('id', '=', ss)])
        for student in mystudents.browse(cr, uid, searching_val, context=context):
            my_description = str(student.name).upper()
        mystudents.write(cr, uid, student.id, {'notes': my_description})

将它放在你的wizard.py