Python 开场白7:AttributeError:“;字段';名称';不存在于对象';浏览记录(向导设置年龄,1)和x27&引用;

Python 开场白7:AttributeError:“;字段';名称';不存在于对象';浏览记录(向导设置年龄,1)和x27&引用;,python,openerp,wizard,openerp-7,odoo,Python,Openerp,Wizard,Openerp 7,Odoo,我有一个向导需要从模块中获取一些字段,但当我尝试执行此操作时,出现以下错误: AttributeError: "Field 'name' does not exist in object 'browse_record(wizard_set_ages, 1)'" 问题是我不知道如何从字段中获取数据。我需要遍历所有选定的记录并执行该操作(取名称并将其写入caps lock中的描述中,如name:Atul->set_description()->description:Atul) 下面是wizard

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

AttributeError: "Field 'name' does not exist in object 'browse_record(wizard_set_ages, 1)'"
问题是我不知道如何从字段中获取数据。我需要遍历所有选定的记录并执行该操作(取名称并将其写入caps lock中的描述中,如name: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()

编辑:添加了向导\u 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>

向导\u设置\u年龄\u表单
向导\u设置\u年龄
形式
老少皆宜
ir.actions.act\u窗口
向导\u设置\u年龄
形式
形式
新的

向导集中没有字段,但您已尝试

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

使用myobj而不是(
myobj=self.pool.get('student.student')
)进行更新,并阅读该表

返回到您的第一个问题“浏览记录向导上不存在名称”,只有两种情况会触发此问题,模型没有名为name或。。。您浏览了一条不存在的记录

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


您的按钮位于具有向导集年龄支持模型的窗体上,这意味着当用户单击它时,它将在向导集年龄模型上执行设置所有年龄。然后执行self.pool.get on student.student并使用ids参数浏览它,但这是您的问题。由于表单位于向导模型上,ids参数包含向导模型的ID,而不是学生模型的ID,因此不能使用它们浏览学生模型。通常的模式是搜索学生ID或在创建向导时将其存储在向导中。请查看选股向导以获取此示例。

尝试此代码,它将起作用

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上

把你的xml代码也放在这里,你可以添加self.write(cr,uid,prod.id,{'notes':my_details}),但是wizard(wizard_set_ages)中没有字段,你需要使用myobj.write(cr,uid,prod.id,{'notes':my_details})代替它添加了向导xml文件,所以我必须通过myobj而不是self.browse进行迭代?另一个功能呢?(set_selected_ages)好的,我已经做了,但是现在它告诉我一个新错误“AttributeError:“在浏览记录(student.student,9)中找不到字段名”我已经更新了主问题的代码是的,谢谢,但是如果我只想设置所选的学生,我能做什么?我需要这两种方法,一种用于设置所有年龄,另一种用于设置所选年龄(仅在列表视图中选择年龄,而不是其他年龄)。不,它仅适用于第一个选择的学生,而不适用于其他学生。“数据”变量的用途是什么?最后一件事,ss变量以“or False”结尾,“why that”,“?