微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

关系字段 Many2one 和 One2many 中的 AttributeError

如何解决关系字段 Many2one 和 One2many 中的 AttributeError

所以我在字段之间创建了一个关系,以便能够在视图中添加多个条目为此上下文 - “这以前不是一个网格,只是一组要填充的字段,但现在我们必须能够添加多个条目”因此我创建了一个新模型以及从“project.project”继承的另一个模型的字段之间的关系

但是当尝试保存条目时会弹出此错误

File "/home/odoo/src/user/rw_project/models/project_adjustments.py",line 36,in _check_discount_value
    if self.total_revenue == 0:
  File "/home/odoo/src/odoo/odoo/fields.py",line 1019,in __get__
    self.compute_value(recs)
  File "/home/odoo/src/odoo/odoo/fields.py",line 1175,in compute_value
    records._compute_field_value(self)
  File "/home/odoo/src/odoo/odoo/models.py",line 4061,in _compute_field_value
    getattr(self,field.compute)()
  File "/home/odoo/src/user/rw_project/models/project_adjustments.py",line 69,in check_project_discount
    if len(record.task_ids) > 0:
Exception

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/odoo/src/odoo/odoo/http.py",line 639,in _handle_exception
    return super(JsonRequest,self)._handle_exception(exception)
  File "/home/odoo/src/odoo/odoo/http.py",line 315,in _handle_exception
    raise exception.with_traceback(None) from new_cause
AttributeError: 'project.adjustments' object has no attribute 'task_ids'

我会把模型放在这里

class MultipleProjectLevel(models.Model):
    _name = "project.adjustments"
    _description = "Project Adjustments"
    
    project_adjustments_id = fields.Many2one('project.project')
    discount_date_end = fields.Date(string='Adjustment end date')
    discount_date_start = fields.Date(string='Adjustment start date')

    discount_type = fields.Selection([
        ('amount','Amount'),('percentage','Percentage')
        ],string='Adjustment type',index=True)
    
    adjustment_type = fields.Selection([
        ('discount','discount'),('markup','Mark-up')
    ],string='discount/Mark-Up',index=True)

    discount_value = fields.Float(digits=(8,2),string="Adjustment value")
    role_amount_adjustment = fields.Float(digits=(8,string="Role Adjustment Value")

    @api.constrains('discount_value','discount_type')
    def _check_discount_value(self):
        if self.discount_type=='percentage':
            if self.discount_value > 100:
                raise exceptions.ValidationError('Adjustment cannot be greater than 100%')
        elif self.discount_type=='amount':
            
                if self.total_revenue == 0:
                    raise exceptions.ValidationError('Total service revenue is zero,an adjustment can\'t be applied')
                    
                if self.discount_date_start and self.discount_date_end:
                    value = self.get_partial_amount()
                else:
                    value = self.total_revenue
                
                if self.discount_value > value:
                    raise exceptions.ValidationError('The amount to Adjustment is greater than the revenue for the date range selected')
                

    @api.constrains('discount_date_start','discount_date_end')
    def check_dates(self):
        """Adjustment End date cannot be before Adjustment Start Date"""
        if self.discount_date_start and self.discount_date_end:
            today = datetime.Now().date()
            if self.discount_date_start < today or self.discount_date_end < today:
                raise exceptions.ValidationError('Date of Adjustment cannot be in the past')

            if self.discount_date_start > self.discount_date_end:
                raise exceptions.ValidationError('Adjustment End date cannot be before Adjustment Start Date')

            if self.discount_date_start < self.date_start:
                 raise exceptions.ValidationError('Adjustment Start date cannot be before Project Start Date')
            
            if self.discount_date_end > self.date:
                 raise exceptions.ValidationError('Adjustment End date cannot be after Project End Date')

    # Process of calculations for the adjustments
    @api.depends('discount_type','discount_value')
    def check_project_discount(self):
        for record in self:
            if len(record.task_ids) > 0:
                record.total_revenue = sum(line.total_service_revenue for line in record.task_ids)
                self.calculate_total_project_adjustment(record)

任何想法都会非常感谢!!

解决方法

如果 task_ids 放在任何其他字段内,那么你可以给 record.field_name.task_ids

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。