2014-06-26 44 views
0

我有一個嚮導,需要從模塊中獲取一些字段,但是當我嘗試執行此操作時出來這個錯誤:Openerp 7:AttributeError:「Field'name'在對象'browse_record(wizard_set_ages,1)'中不存在''

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

把你的XML代碼也在這裏 – senthilnathang

+0

在嚮導添加self.write(CR,UID,prod.id,{ '註釋':my_details}),但有在嚮導中沒有字段(wizard_set_ages),你需要使用myobj.write(cr,uid,prod.id,{'notes':my_details})而不是那個 – senthilnathang

+0

添加嚮導xml文件 – barbaanto

回答

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

+0

是的,謝謝,但是如果我想只設置選定的學生,我該怎麼辦?我需要所有的兩個方法,一個用於set_all_ages,另一個用於set_selected_ages(僅在列表視圖中選擇的年齡而不是其他) – barbaanto

+0

嘗試更新的代碼。 – user3736293

+0

不是,它只適用於第一個選擇的學生而不適用於其他人 – barbaanto

0

中有wizard_set_ages沒有字段,但你試過

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

使用而不是(myobj = self.pool.get('student.student'))MyObj中用於更新和讀取表

+0

所以我必須迭代槽myobj self.browse?併爲其他功能? (set_selected_ages) – barbaanto

+0

好吧,我已經做到了,但現在它對我說一個新的錯誤「AttributeError:'在browse_record中找不到字段名稱(student.student,9)'」我更新了主要問題上的代碼 – barbaanto

0

再回到你的第一個問題「的名字不會在瀏覽記錄嚮導存在「,只有兩件事情可以觸發這種情況,模型沒有名稱列或...您瀏覽過一個不存在的記錄。

然而,在你的第二個問題..

你的按鈕是wizard_set_ages的後備模式,當用戶點擊它,這意味着在窗體上,它執行上設置wizard_set_ages模型中所有年齡。然後,您在student.student上執行self.pool.get並使用ids參數進行瀏覽,但這是您的問題。由於表單位於嚮導模型中,因此ids參數包含嚮導模型的id,而不是學生模型,因此您無法使用它們瀏覽學生模型。通常的模式是在創建時搜索學生ID或將其存儲在嚮導中。看看股票選擇嚮導的例子。

相關問題