2017-12-02 190 views
2

我有一個形式的按鈕,單擊時應該更新從spray.action模型數據當前模型mrl。然後,我做進一步的處理,但它引發錯誤爲什麼我得到這個錯誤?:預計單:spray.action(1,2)

ValueError("Expected singleton: %s" % self) ValueError: Expected singleton: spray.action(1, 2) 
@api.multi 
def mrlCreateSprayRecords(self): 
    spray_ids = [] 
    vals = [] 
    spray_obj = self.env['spray.action'].search([]) 
    print("spray_obj \n\n\n\t %s ", spray_obj) 
    for obj in spray_obj: 
     print("Spray Action Objects \n\n %s \n\t ", obj) 
     vals = { 
      'ref': obj.ref, 
      'farm': obj.farm.farm, 
      'block': obj.block.block, 
      'valves': obj.valves.valve, 
     } 
     print("Spray Action Data Browse , \n\n\t %s ", vals) 
     res = super(Mrl, self).create(vals) 
     res.update(vals) 
     print("object in mrlCreateSprayRecords \n\n\t %s", res) 
    return { 
     'name': 'Update Mrl Operations', 
     'type': 'ir.actions.act_window', 
     'view_type': 'form', 
     'view_mode': 'form', 
     'res_model': 'mrl', 
     'views': [(spray_obj.id, 'form')], 
     'view_id': spray_obj.id, 
     # 'target': 'new', 
     'res_id': self.id, 
     'context': self.env.context, 

    } 
+0

下一次試着問用更少的噪音,以幫助讀者解答,請:P – ChesuCR

+0

只需在搜索中添加上限= 1,如。 spray_obj = self.env ['spray.action']。search([],limit = 1) –

回答

4

我想你'view_id': spray_obj.id,必須有寫視圖id的行中得到的錯誤。 spray_obj記錄集有很多記錄,所以你不能像那樣使用它(spray_obj.id)。您也可以刪除view_id參數以便使用默認視圖。

@api.multi 
def mrlCreateSprayRecords(self): 
    self.ensure_one() 

    spray_obj = self.env['spray.action'].search([])  # recordset of all records of the model???? 
    for obj in spray_obj: 
     vals = { 
      'ref': obj.ref, 
      'farm': obj.farm.farm, 
      'block': obj.block.block, 
      'valves': obj.valves.valve, 
     } 
     self.create(vals) 

    view_id = self.env.ref('module.xml_view_id').id   

    return { 
     'name': 'Update Mrl Operations', 
     'type': 'ir.actions.act_window', 
     'view_type': 'form', 
     'view_mode': 'form', 
     'res_model': 'mrl', 
     'view_id': view_id, 
     'res_id': self.id, 
     'context': self.env.context, 
    } 

我已經加入self.ensure_one(),因爲res_id必須只有一個ID爲好。

我刪除res.update(vals)線,因爲它沒有任何意義,我哈哈

更多的事情

而是打印的,你應該使用記錄器:

import logging 
_logger = logging.getLogger(__name__) 

_logger.info('Hello') 

相反,行res = super(Mrl, self).create(vals)我認爲你應該使用這一個

res = self.create(vals)  # if you are in the mrl model 
2

總之,當您訪問包含多條記錄的recordSet的字段direclty時,會出現此類錯誤。

您對搜索returnd 2記錄spray.action執行搜索(1, 2)

所以,當你做spray_obj.id odoo會感到困惑什麼ID,他應該返回1或2,而這裏odoo拋出這個錯誤。

所以不要訪問一個字段的搜索結果,或x2many fiels他們可能有多個記錄裏面。

@ChesuCR有所改善你的代碼並糾正它

相關問題