Odoo单身错误。怎么解决?

时间:2018-04-12 11:56:01

标签: odoo

这是我的代码:

    @api.depends('rel_id')
    @api.multi
    def payment_amount(self):
        if(self.rel_id.amount<self.amount_paid):
            raise ValidationError('Please enter correct amount')
        elif(self.rel_id.amount==self.amount_paid):
            self.remaining_amount=0
        elif(self.rel_id.amount>self.amount_paid):
            self.remaining_amount=self.rel_id.amount-self.amount_paid
        for rec in self.rel_id.purchase:
            if(rec.rel_id.amount>rec.amount_paid):
                rec.remaining_amount=rec.remaining_amount-rec.amount_paid

它给了我这个错误

 File "/home/iswasu/odoo-10.0-c-20170418/odoo/models.py", line 4814, in ensure_one
 raise ValueError("Expected singleton: %s" % self)
 ValueError: Expected singleton: customer.sales.order(20, 22) 

1 个答案:

答案 0 :(得分:0)

当你使用装饰多或总是依赖 循环自我,不直接访问它的属性

     self.rel_id.amount # if self contains more than one record you will get this error

所以改变你的代码:

# you don't have to specify @api.multi because it's by default
# when you use @api.depends
@api.depends('rel_id')
def payment_amount(self):
    # remembre don't access self.some_field_name_directly because you will have that error if the 
    # self contains more than one record
    for rec in self: 
        # here rec is always single record it's safe to access it's properties
        if(rec.rel_id.amount < rec.amount_paid):
            raise ValidationError('Please enter correct amount')
        elif(rec.rel_id.amount == rec.amount_paid):
            rec.remaining_amount=0
        elif(rec.rel_id.amount > rec.amount_paid):
            rec.remaining_amount = rec.rel_id.amount-rec.amount_paid