关系字段 Many2one 和 One2many 中的 AttributeError

时间:2021-05-25 22:17:17

标签: python odoo odoo-14

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

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

1 个答案:

答案 0 :(得分:0)

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