2017-11-22 3 views
2

in account.analytic.lines私はフィールド番号を持っています。すべてのプロジェクトにはnumerフィールドがあります。 目標は、行内のnumberフィールドに入力すると、行内にある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} 

答えて

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 _ 
+0

あなたのユーザーのための警告メッセージを追加することも私の編集をチェックします – Cherif