Python/Odoo10: AttributeError: 'dict' object has no attribute '_sale_postprocess'

Python/Odoo10: AttributeError: 'dict' object has no attribute '_sale_postprocess'

我是 python 的新手,不知道如何解决这个错误。请指导我。我想对 'unit_amount' 浮点字段进行验证,如果该字段的值大于 8,那么它应该给出一条错误消息并且不应保存该值,尽管我单击了 'Save' 按钮。

我收到以下错误:

Traceback (most recent call last):   File "C:\odooenv\venv\Scripts\odoo\http.py", line 653, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)   File "C:\odooenv\venv\Scripts\odoo\http.py", line 312, in
_handle_exception
    raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])   File "C:\odooenv\venv\Scripts\odoo\tools\pycompat.py", line 87, in reraise
    raise value   File "C:\odooenv\venv\Scripts\odoo\http.py", line 695, in dispatch
    result = self._call_function(**self.params)   File "C:\odooenv\venv\Scripts\odoo\http.py", line 344, in _call_function
    return checked_call(self.db, *args, **kwargs)   File "C:\odooenv\venv\Scripts\odoo\service\model.py", line 97, in wrapper
    return f(dbname, *args, **kwargs)   File "C:\odooenv\venv\Scripts\odoo\http.py", line 337, in checked_call
    result = self.endpoint(*a, **kw)   File "C:\odooenv\venv\Scripts\odoo\http.py", line 939, in __call__
    return self.method(*args, **kw)   File "C:\odooenv\venv\Scripts\odoo\http.py", line 517, in response_wrap
    response = f(*args, **kw)   File "c:\odooenv\venv\scripts\odoo\addons\web\controllers\main.py", line 934, in call_kw
    return self._call_kw(model, method, args, kwargs)   File "c:\odooenv\venv\scripts\odoo\addons\web\controllers\main.py", line 926, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)   File "C:\odooenv\venv\Scripts\odoo\api.py", line 697, in call_kw
    return call_kw_model(method, model, args, kwargs)   File "C:\odooenv\venv\Scripts\odoo\api.py", line 682, in call_kw_model
    result = method(recs, *args, **kwargs)   File "c:\odooenv\venv\scripts\odoo\addons\sale\models\analytic.py", line 16, in create
    result._sale_postprocess(values) AttributeError: 'dict' object has no attribute '_sale_postprocess'

代码如下:

from odoo import api, fields, models

class AccountAnalyticLine(models.Model):
    _inherit = 'account.analytic.line'

    @api.model
    def default_get(self, field_list):
        result = super(AccountAnalyticLine, self).default_get(field_list)
        if 'employee_id' in field_list and result.get('user_id'):
            result['employee_id'] = self.env['hr.employee'].search([('user_id', '=', result['user_id'])], limit=1).id
        return result

    task_id = fields.Many2one('project.task', 'Task', index=True)
    project_id = fields.Many2one('project.project', 'Project', domain=[('allow_timesheets', '=', True)])

    employee_id = fields.Many2one('hr.employee', "Employee")
    department_id = fields.Many2one('hr.department', "Department", compute='_compute_department_id', store=True, compute_sudo=True)

    @api.onchange('project_id')
    def onchange_project_id(self):
        # reset task when changing project
        self.task_id = False
        # force domain on task when project is set
        if self.project_id:
            return {'domain': {
                'task_id': [('project_id', '=', self.project_id.id)]
            }}

    @api.onchange('employee_id')
    def _onchange_employee_id(self):
        self.user_id = self.employee_id.user_id

    @api.depends('employee_id')
    def _compute_department_id(self):
        for line in self:
            line.department_id = line.employee_id.department_id

    @api.model
    def create(self, vals):
        # compute employee only for timesheet lines, makes no sense for other lines
        match = vals.get('unit_amount')
        if match > 8:
            return{
                'warning': {
                    'title': "Validation Box",
                    'message': "The unit amount should not be more than 8 hours per project and Standard 40 Hours/Week",
                },
            }
        else:
            if not vals.get('employee_id') and vals.get('project_id'):
                if vals.get('user_id'):
                    ts_user_id = vals['user_id']
                else:
                    ts_user_id = self._default_user()
                vals['employee_id'] = self.env['hr.employee'].search([('user_id', '=', ts_user_id)], limit=1).id

            vals = self._timesheet_preprocess(vals)
            return super(AccountAnalyticLine, self).create(vals)

    @api.multi
    def write(self, vals):
        vals = self._timesheet_preprocess(vals)
        return super(AccountAnalyticLine, self).write(vals)

    def _timesheet_preprocess(self, vals):
        """Deduce other field values from the one given.
            Overrride this to compute on the fly some field that can not be computed fields.
            :param values: dict values for `create`or `write`.
        """
        # project implies analytic account
        if vals.get('project_id') and not vals.get('account_id'):
            project = self.env['project.project'].browse(vals.get('project_id'))
            vals['account_id'] = project.analytic_account_id.id
        # employee implies user
        if vals.get('employee_id') and not vals.get('user_id'):
            employee = self.env['hr.employee'].browse(vals['employee_id'])
            vals['user_id'] = employee.user_id.id
        return vals

忘记覆盖 ORM create 方法。相反,以这种方式创建约束:

from odoo import api, fields, models, _
from odoo.exceptions import ValidationError


class AccountAnalyticLine(models.Model):
    _inherit = 'account.analytic.line'

    ...

    @api.multi
    @api.constrains('unit_amount')
    def _check_unit_amount(self):
        for line in self:
            if line.unit_amount > 8:
                raise ValidationError(
                    _('The unit amount should not be more than 8 hours '
                      'per project and Standard 40 Hours/Week')
                )