自动填充字段

时间:2017-11-22 08:06:30

标签: openerp odoo-8 odoo-9

在account.analytic.lines中的

我有字段编号。每个项目都有数字领域。 所以我的目标是当我在行中输入数字字段时自动填充行中的project_id字段。

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

    number = fields.Integer(related='project_id.number',string='Project Number')

    @api.onchange('number')
    def get_project_id(self):
        v={}
        if self.number:
            project = self.env['project.project']
            if project.project_id.id:
                v['project'] = project.project_id and project.project_id.id or False
        return {'value': v}

1 个答案:

答案 0 :(得分:2)

试试这个:

 @api.onchange('number')
 def get_project_id(self):
    # in new api no need for return and you can
    # affect change directly to self
    project = false
    if self.number:
        project_obj = self.env['project.project']
        # now search for project that have the same number
        porject = project_obj.search([('number', '=', self.number)], limit=1)

    self.project_id = project
    # if you want to show a warning when user fillup
    # the number field and there is no project found
    if not self.project_id and self.number:
        # number is not empty but there is no project with this number
        return {'warning': {
                        'title': _("Project Warning"),
                        'message': _('No project found with this number : %s ') % self.number
                    }}

不要忘记导入翻译toos:

# 10.0
from odoo.tools.translate import _