2017-08-12 27 views
0

我需要重写create方法在我的模型上odoo 10:覆盖实现方法具odoo 10不工作

我的模块中

我有三个型号:

资产随着

validated = fields.Boolean("Is validated") 
survey2_ids = fields.One2many('mymodule.survey2', 'asset_id', string='Survey2') 

Survey2有:

name = fields.Char() 
asset_id = fields.Many2one('asset.asset', description='Asset') 
survey1_id = fields.Many2one('mymodule.survey1', description="Survey1") 
description = fields.Text(description="description") 

调查1具有:

name = fields.Char(description="Name") 
ok = fields.Boolean("Is ok") 
description = fields.Text() 

在这里的目标是创建一个时新的资产,如果验证=真:在mymodule.survey1的所有记录点击OK ==真应该survey2_ids被复制,我想这功能,但它似乎没有是工作:

@api.model 
def create(self, vals): 
    survey1_ids = self.env['mymodule.survey1'].search([('ok', '=', True)]) 
    if self.validated: 
     for rec in survey1_ids: 
      vals['survey2_ids'] = [(0, False, {'asset_id': self.id, 'survey2_id': rec.id,'name':rec.name,'description':})] 
    return super(asset_asset, self).create(vals) 

任何帮助将aappreciated

回答

1

有两个问题在你的代码:

  1. 创建是怎样的一个“类方法“(它与模型绑定,不符合记录)。因此,当您询问self.validated的值时,这总是会是错误的,因为自己不是您创建的记录,而是模型。您应该检查vals.get('validated')。或者先手动创建记录,然后用它代替自己(在我的示例中,在新创建的记录中为res)。
  2. 你不是真的抄袭调查1到测量3.你必须在调查中使用的数据创建调查2 1.

,我认为是最好的解决办法:

@api.model 
def create(self, vals): 
    res = super(asset_asset, self).create(vals) 
    if vals.get('validated'): 
     survey1_ids = self.env['mymodule.survey1'].search([('ok', '=', True)]) 
     for s in survey1_ids: 
      v = { 
       'name': s.name, 
       'description': s.description, 
       'survey1_id': s.id, 
       'asset_id': res.id 
      } 
      self.env['mymodule.survey2'].create(v) 
    return res 
+0

它聪明地工作!非常感谢你的帮助! –

+0

@JihaneHemicha不客气。谢谢。 – Majikat

0

假设有在日志中没有错误,你没有得到你究竟想要做的事。一旦代码执行完毕,您只能获得1项与该资产相关的调查。

这是因为创建函数内部,你写道:

vals['survey2_ids'] = [(0, False, {'asset_id': self.id, 'survey2_id': rec.id,'name':rec.name,'description':})]

这每一次都将覆盖survey2_id在丘壑在for循环。

你应该在这里做的是:

survey_2_list = [] 
for rec in survey1_ids: 
    survey_2_list.append((0, False, {'asset_id': self.id, 'survey2_id': rec.id,'name':rec.name,'description':rec.description})) 
vals['survey2_ids'] = survey_2_list 
0

尝试以下操作:

@api.model 
def create(self, vals): 
    survey_2_list = [] 
    if self.validated: 
     survey1_ids = self.env['mymodule.survey1'].search([('ok', '=', True)]) 
     if survey1_ids: 
      for rec in survey1_ids: 
       values = { 
        'asset_id': self.id, 
        'survey2_id': rec.id, 
        'name':rec.name, 
        'description':rec.description, 
        } 
       survey_2_list.append((0, False, values)) 
      vals['survey2_ids'] = survey_2_list 
    return super(asset_asset, self).create(vals)