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

Python / Odoo10:AttributeError:'dict'对象没有属性'_sale_postprocess'

如何解决Python / Odoo10:AttributeError:'dict'对象没有属性'_sale_postprocess'

我是python的新手,不确定如何解决错误。请指导我。我想对'unit_amount'浮点型字段进行验证,如果该字段的值大于8,那么它应该给出错误消息,并且即使单击“保存”按钮也不应保存该值。

我收到以下错误

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,**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,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],kwargs)   File "C:\odooenv\venv\Scripts\odoo\api.py",line 697,in call_kw
    return call_kw_model(method,model,line 682,in call_kw_model
    result = method(recs,**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')
                )

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