2017-10-12 62 views
1

我正在使用odoo 10e。我想要做的就是我想設置裏面fields_view_get方法Odoo - 如何從後端設置域名過濾器

@api.model 
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False): 
    res = super(Customer, self).fields_view_get(
     view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu) 
    doc = etree.XML(res['arch']) 
    if view_type == 'tree': 
     if self.env.user.partner_id.parent_id.id is False: 
      id = self.env.user.id 
     else: 
      id = self.env.user.partner_id.parent_id.id 
     doc.attrib['domain'] = "[('custodians','='," + str(id) + ")]" 

    for node_form in doc.xpath("//tree"): 
     node_form.attrib['duplicate'] = '0' 
     res['arch'] = etree.tostring(doc) 
    for node_form in doc.xpath("//form"): 
     node_form.attrib['duplicate'] = '0' 
     res['arch'] = etree.tostring(doc) 
    return res 

這就是我所做的嘗試域標準。但它不工作。你可以看到爲什麼我想從後端設置域,因爲我必須根據條件設置user_id。

請讓我知道如果我做錯了或有什麼更好的方法。

編輯

我已經定義託管領域遵循

custodians = fields.Many2one('res.users', string="Custodian", domain=[('groups_id', 'in', [12])], 
          readonly=[('readonly_custodian', '=', True)]) 

實際上當過一的loggedIn用戶創建Customer記錄,我們把他作爲該Customer保管人和所有我想做的事是當用戶再次登錄時,他應該能夠看到他和他的父母監護人記錄

+0

你應該很少需要重寫'fields_get'。你究竟想達到什麼目的? – travisw

+0

我想從後端動態地在樹視圖上添加域過濾器。所以我可以有基於條件的域名過濾器 – Ancient

+0

看到這個檢查下的所有東西'if view_type =='tree''。這是我想要在域 – Ancient

回答

2

看來你應該能夠實現這一點通過修改加載您的樹形視圖的action上的domain來實現。

有兩種方法可以定義Window Actions,但無論哪種方式,重要的部分是domain值。

老答案:

第一種方式:

<act_window id="..." 
      name="..." 
      ... 
      domain="[('custodians','=',user.partner_id.parent_id and user.partner_id.parent_id.id or user.id)]"/> 

方式二:

<record id="..." model="ir.actions.act_window"> 
    <field name="domain">[('custodians','=',user.partner_id.parent_id and user.partner_id.parent_id.id or user.id)]</field> 
</record> 

編輯:

因爲它似乎user不在domain訪問,我想這可能工作:

Record Rule

<record id="view_ir_rule_restrict_custodians" model="ir.rule"> 
    <field name="name">Restrict Users to see only their Custodians</field> 
    <field name="model_id" ref="model_res_partner"/> 
    <!-- Should this be `... or user.partner_id.id`? --> 
    <field name="domain_force">[('custodians','=',user.partner_id.parent_id and user.partner_id.parent_id.id or user.id)]</field> 
</record> 

注:這是一個全球性的規則,所以你可能要強行刪除對某些羣體(如經理)

<record id="view_ir_rule_unrestrict_custodians_managers" model="ir.rule"> 
    <field name="name">Un-Restrict Managers to see any Custodians</field> 
    <field name="model_id" ref="model_res_partner"/> 
    <field name="domain_force">[(1, '=', 1)]</field> 
    <field name="groups" eval="[(4, ref('base.group_sale_manager'))]"/> 
</record> 

編輯2:

正如我們在聊天中發現,具體到你的問題域是這樣的:

[('custodians','in',user.partner_id.parent_id and [(user.partner_id.parent_id.id),user.partner_id.id] or [user.partner_id.id])] 
+0

實現的條件以及我認爲'用戶'是不可用的域..讓我仔細檢查它 – Ancient

+0

是的,因爲我說用戶對象不可用,看到我得到這個錯誤'未捕獲的錯誤: NameError:名稱'用戶'未定義' – Ancient

+0

@Ancient您可以嘗試它作爲'uid'而不是'user'嗎?我認爲這只是一個整數,而不是一個對象,所以你可能仍然會有一個錯誤。如果你可以使用一個簡單的'[('custodians','=',uid)]',那麼你應該可以採用不同的路線。讓我知道,如果它與'uid'一起工作,我會更新我的答案。 – travisw