自动填充字段

Auto fill field

在 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}

试试这个:

 @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
                    }}

别忘了导入翻译:

# 10.0
from odoo.tools.translate import _