2016-08-30 20 views
2

在hr出席中,有一個名爲「employee_id」的字段。如何設置只能爲一個組編輯的字段?在odoo9

我想設置此字段只能編輯一個組(或設置爲只讀其他組)。

例如,我想只在「管理員」組中設置「表單」視圖中可編輯的字段「employee_id」。

我已經擴展了考勤模塊,我有這樣的代碼在我的擴展模塊的XML:

<?xml version="1.0" encoding="utf-8"?> 
<openerp> 
    <data> 
    <record id="view_employee_readonly_custom" model="ir.ui.view"> 
     <field name="name">hr.attendance.form</field> 
     <field name="model">hr.attendance</field> 
     <field name="inherit_id" ref="hr_attendance.view_attendance_form"/> 
     <field name="groups_id" eval="[(6,0,[ref('base.group_hr_manager')])]"/> 
     <field name="arch" type="xml"> 
      <field name="employee_id" position="attributes"> 
       <attribute name="readonly">True</attribute> 
      </field> 
     </field> 
    </record> 
    </data> 
</openerp> 

有了這個代碼,本場可編輯爲大家除了hr_manager組。這是我想要的。

我爲了達到這個目的需要修改什麼?

編輯:我修改了不同字段的原始代碼以便更好地理解。

回答

1

我找到了!

首先,該字段必須定義爲只讀爲每個人。

<xpath expr="//field[@name='employee_id']" position="replace"> 
    <field name="employee_id" attrs="{'readonly':True}"/> 
</xpath> 

然後,我們繼承了第一個自定義視圖

<field name="inherit_id" ref="hr_attendance_extend.view_employee_readonly_custom"/> 

最後,我們刪除該管理員組的只讀限制(group_hr_manager)

<field name="groups_id" eval="[(6, 0, [ref('base.group_hr_manager')])]"/> 
<field name="arch" type="xml"> 
    <xpath expr="//field[@name='employee_id']" position="attributes"> 
     <attribute name="readonly">False</attribute> 
    </xpath> 
</field> 

下面是最終代碼:

<?xml version="1.0" encoding="utf-8"?> 
<openerp> 
    <data> 
    <record id="view_employee_readonly_custom" model="ir.ui.view"> 
     <field name="name">hr.attendance.form</field> 
     <field name="model">hr.attendance</field> 
     <field name="inherit_id" ref="hr_attendance.view_attendance_form"/> 
     <field name="arch" type="xml"> 
      <xpath expr="//field[@name='employee_id']" position="replace"> 
       <field name="employee_id" attrs="{'readonly':True}"/> 
      </xpath> 
     </field> 
    </record> 

    <record id="view_employee_readonly" model="ir.ui.view"> 
     <field name="name">hr.attendance.form</field> 
     <field name="model">hr.attendance</field> 
     <field name="inherit_id" ref="hr_attendance_extend.view_employee_readonly_custom" /> 
     <field name="groups_id" eval="[(6, 0, [ref('base.group_hr_manager')])]"/> 
     <field name="arch" type="xml"> 
      <xpath expr="//field[@name='employee_id']" position="attributes"> 
       <attribute name="readonly">False</attribute> 
      </xpath> 
     </field> 
    </record> 
    </data> 
</openerp> 
1

如果我沒有記錯的話,Odoo中沒有內置的方法可以讓某個字段只能編輯某個字段。

可以通過向其添加組使其可見或不可見。

如果您希望根據組使用該字段進行編輯,則需要創建一個與用戶相關的新計算字段,並在該字段上添加attrs以使其基於用戶進行只讀。

在你的情況,你需要這樣的事:

在蟒蛇:

can_edit_name = fields.Boolean(compute='_compute_can_edit_name') 

def _compute_can_edit_name(self): 
    self.can_edit_name = self.env.user.has_group('base.group_hr_user') 

在你的XML:

<xpath expr="//field[@name='name']" position="before"> 
    <field name="can_edit_name" invisible="1"/> 
</xpath> 
<xpath expr="//field[@name='name']" position="attributes"> 
    <attribute name="attrs">{'readonly': ['can_edit_name', '=', False]}</attribute> 
</xpath> 

這意味着,如果can_edit_name爲True,字段將是可編輯的。

我還沒有測試過,所以可能會出現一些錯別字,但是這應該會讓你知道如何去做!

祝你好運!

+0

我不想使用計算場。現在,我找到了解決方案。無論如何,謝謝你的回答:) – MouTio

相關問題