循环通过One2Many记录Odoo 10

时间:2017-11-16 11:26:12

标签: python openerp odoo-10

我正在尝试遍历我的One2Many记录以避免重复。

class sales_target(models.Model):
    _name = 'sales.target'
    _description = 'Sales Target'

    name = fields.Char(string='Name',required=True)
    from_date = fields.Date(string='From Date',required=True)
    to_date = fields.Date(string='To Date',required=True)
    sales_team = fields.Many2one('crm.team',required=True)
    sales_record_ids = fields.One2many('sales.target.record','sales_target_rec_id',string='Sales Record')  

    @api.one   
    def check_duplication(self,result):   
        count = 0
        if self.sales_record_ids:    
            for record in self.sales_record_ids:
                if result.id == record.sales_person_p_id:                  
                    count = 1
        if count == 0:  
            self.write({'sales_record_ids':[(0,0,{'sales_person':result.name})]})     

    @api.one    
    def get_sales_person(self): 
        for res in self.sales_team.member_ids:            
            self.check_duplication(res)         

另一个类是:

class sales_target_record(models.Model):
    _name = 'sales.target.record'

    sales_target_rec_id = fields.Many2one("sales.target")    
    sales_person = fields.Char(string='Sales Person',readonly=True,required=True)
    sales_person_p_id = fields.Char(compute='get_value',store=True)



 @api.onchange('sales_person')
    @api.depends('sales_person')
    def get_value(self):
        res = self.env['res.partner'].search([('name','=',self.sales_person)])          
        self.sales_person_p_id = res[0].id

现在,当我按下按钮时,我仍然有重复的记录。然而,我试图比较名称和事情工作良好,但我不能与名称比较,因为它不正确,因为名称可以相同,但ID不能。该功能如下:

 @api.one   
 def check_duplication(self,result):   
     count = 0
     if self.sales_record_ids:            
         for record in self.sales_record_ids:
             if result.name == record.sales_person:                    
                 count = 1
     if count == 0:         
         self.write({'sales_record_ids':[(0,0,{'sales_person':result.name})]})    

希望得到指导。

2 个答案:

答案 0 :(得分:0)

你能尝试这样吗

@api.multi   
def check_duplication(self,result):   
    if self.sales_record_ids:            
       for record in self.sales_record_ids:
           if not result.name == record.sales_person:                           
              self.write({'sales_record_ids':[(0,0,{'sales_person':result.name})]})    

答案 1 :(得分:0)

name它正常运作的事实可以得出结论,if条件可能有问题。

sales_person_p_id的类型为char,但您似乎将其与整数进行比较:result.id

您是否确保if条件中的两个对象都是type

尝试将sales_person_p_id设为整数字段(例如,通过sales_person_p_id = fields.Integer(compute='get_value',store=True)或在比较对象之前进行某种类型的转换。