2017-10-11 46 views
0

我不知道我的代碼有什麼問題。我有兩個模型,一個是payroll.adjustment,我在我的xml中使用模型,一個是payroll.adjustment.lines。我所需要做的就是在payroll.adjustment.lines的employee_id字段中創建一個域,並在此處執行此操作。使用Python創建從頭到細節的域odoo 10

<field name="employee_id" options="{'no_create': True}" domain=" 
[('address_id','=',company_id)]"/> 

,但它顯示了一個錯誤 「未捕獲的錯誤:NameError:名字 'COMPANY_ID' 沒有定義」

我在的.py

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

class PayrollAdjustment(models.Model): 
    _name = 'payroll.adjustment' 
    _description ='Payroll Adjustment' 

    name = fields.Char(string="Name", related='doc_num') 
    doc_num = fields.Char(string="Document No.") 
    state = fields.Selection([('draft', 'Draft'), 
          ('confirm', 'Waiting for Approval'), 
          ('approved','Approved'), 
          ('void', 'Void')],default='draft') 
    company_id = fields.Many2one('res.partner',string="Company",domain=" 
    [('is_company','=',True)]") 
    date_from = fields.Date(string="Date From", required= True) 
    date_to = fields.Date(string="Date To", required= True) 
    adjustment_lines = 
    fields.One2many('payroll.adjustment.lines','adj_id',string="Adjustment 
    lines") 
    color = fields.Integer() 
    sample = fields.Many2one ('hr.employee') 

    @api.multi 
    def action_draft(self): 
     self.state = 'draft' 

    @api.multi 
    def action_confirm(self): 
     self.state = 'confirm' 

    @api.multi 
    def action_approve(self): 
     self.state = 'approved' 

    @api.multi 
    def action_void(self): 
     self.state = 'void' 

    @api.model 
    def create(self, vals): 
     vals['name'] = self.env['ir.sequence'].get('payroll.adj.seq') 

     return super(PayrollAdjustment, self).create(vals) 

class PayrollAdjustmentLines(models.Model): 
    _name = 'payroll.adjustment.lines' 
    _description = 'Payroll Adjustment Lines' 
    _inherit = ['mail.thread'] 

    employee_id = fields.Many2one('hr.employee',  
    string="Employee",required=True) 
    adjustment_for = fields.Many2one('hr.salary.rule', string="Adjustment 
    for",domain=[('appears_on_adj', '=', True)], required=True) 
    remarks = fields.Char(string="Remarks") 
    amount = fields.Float(string="Amount", default = 0.00) 
    adj_id = fields.Many2one('payroll.adjustment',string="Payroll 
    Adjustment",ondelete='cascade') 

我的代碼的.xml代碼

<record id="payroll_adjustment_form_view" model="ir.ui.view"> 
     <field name="name">payroll_adjustment.form</field> 
     <field name="model">payroll.adjustment</field> 
     <field name="arch" type="xml"> 
      <form> 
       <header> 
        <button name="action_draft" type="object" string="Set 
back to draft" states="confirm"/> 
        <button name="action_confirm" type="object" 
string="Confirm" states="draft" class="oe_highlight"/> 
        <button name="action_approve" type="object" 
string="Approve" states="confirm" class="oe_highlight" 
groups="hr_payroll.group_hr_payroll_manager"/> 
        <button name="action_void" type="object" string="Void" 
states="confirm,approved" class="oe_highlight" 
groups="hr_payroll.group_hr_payroll_manager"/> 
        <field name="state" widget="statusbar"/> 
       </header> 
       <sheet> 
        <div class="pull-left"> 
         <label for="doc_num" class="oe_edit_only oe_inline"/> 
         <h1> 
          <field name="doc_num" readonly="1"/> 
         </h1> 
        </div> 
        <group> 
         <label for="company_id"/> 
         <div> 
          <field name="company_id" class="oe_inline" 
attrs="{'readonly': [('state','not in',('draft'))]}"/> 
         </div> 

         <label for="date_from" string="Period"/> 
         <div> 
          <field name="date_from" class="oe_inline" 
attrs="{'readonly': [('state','not in',('draft'))]}"/> - 
          <field name="date_to" class="oe_inline" attrs=" 
{'readonly': [('state','not in',('draft'))]}"/> 
          <field name="sample" domain=" 
[('address_id','=',company_id)]"/> 
         </div> 
        </group> 
        <field name="adjustment_lines" attrs="{'readonly': 
[('state','not in',('draft'))]}" context="{'company': company_id }"> 
         <tree editable="1"> 
          <field name="employee_id" options="{'no_create': 
True}" domain="[('address_id','=',company_id)]"/> 
          <field name="adjustment_for" options=" 
{'no_create': True}"/> 
          <field name="amount"/> 
          <field name="remarks"/> 
         </tree> 
        </field> 
       </sheet> 
      </form> 
     </field> 
    </record> 

請幫幫我。在此先感謝

回答

1

您正在'payroll.adjustment.lines'模型的字段employee_id中添加域,但'company_id'字段不在該模型中,但它在父模型'payroll.adjustment'中。

所以,你應該寫如下:

<field name="employee_id" options="{'no_create': True}" domain=" 
[('address_id','=',parent.company_id)]"/> 
+0

它的工作原理。非常感謝你 –

+0

你好先生/女士。如果我想在payroll.adjustment的樹形視圖上顯示employee_id,可以嗎?我可以添加什麼?這是對的嗎? ? –