2017-07-12 179 views
2

我添加 'LOCATION_ID' 字段樹視圖res.user到用戶連接到某個位置如何篩選與動態字段odoo-10

類ResUser(models.Model): _inherit ='res.users '

current_location_id=fields.Many2one('physical.location',string="Current Loction") 
allowed_location_ids=fields.Many2many('physical.location',string="Allowed Location") 

我想篩選只與current_location_id = user_current_location_id

<filter string="My visits" name="filter_my_visits" 
         domain="[('current_location_id','=',current_location_id)]"/> 

其給出該錯誤消息中的訪問

Uncaught Error: Failed to evaluate search criterions: 
{"code":400,"message":"Evaluation Error","data":{"type":"local_exception","debug":"Local evaluation failure\nNameError: name 'current_location_id' is not defined\n\n{\"domains\":[[],\"[('current_location_id','=',current_location_id)]\"],\"contexts\":[{\"lang\":\"en_US\",\"tz\":false,\"uid\":1,\"params\":{\"action\":354,\"min\":1,\"limit\":80,\"view_type\":\"list\",\"model\":\"visit.visit\",\"menu_id\":241,\"_push_me\":false}},{}],\"group_by_seq\":[]}"}} 

鑑於唯一可用的領域直接調用的UID郎...等 所以我試圖重寫閱讀方法來更新背景,但它似乎並沒有在視圖中進行更新,仍然給未定義 'current_location_id'

@api.multi 
    def read(self, fields=None, load='_classic_read'): 
     ctx = self.env.context.copy() 
     ctx.update({ 'user_current_location_id':self.env.user.current_location_id.id}) 

     return super(Visit,self.with_context(ctx)).read(fields=fields,load=load) 

visit_tree_view

<record id="visit_visit_tree_view" model="ir.ui.view"> 
    <field name="name">visit.visit.tree</field> 
    <field name="model">visit.visit</field> 
    <field name="arch" type="xml"> 
     <tree string="Visits"> 
      <field name="name" /> 
      <field name="date"/> 
      <field name="visit_type" /> 
      <field name="patient_id"/> 
      <field name="hcp_id"/> 
      <field name="current_location_id" /> 
      <field name="user_current_location_id"/> 
      <field name="severity"/> 
      <field name="state"/> 
     </tree> 
    </field> 
</record> 

六坐行動

<record id="visit_visit_act" model="ir.actions.act_window"> 
    <field name="name">Visit</field> 
    <field name="res_model">visit.visit</field> 
    <field name="view_type">form</field> 
    <field name="view_mode">tree,form</field> 
    <field name="context">{'search_default_filter_my_visits':1}</field> 
    <field name="search_view_id" ref="view_visit_filter"/> 
</record> 

這裏是我做過什麼:

@api.multi 
def vsit_action(self): 
    ctx = self.env.context.copy() 

    ctx.update({'search_default_current_location_id': self.env.user.current_location_id.id, 
       'user_current_location_id': self.env.user.current_location_id.id}) 
    return { 
     'name': 'Visit', 
     'view_type': 'form', 
     'view_mode': 'tree,form', 
     'res_model': 'visit.visit', 
     'type': 'ir.actions.act_window', 
     'context': dict(ctx), 
     'search_view_id': self.env.ref('eroyal_visit.view_visit_filter').id 
    } 

過濾器:

<filter string="My visits" name="filter_my_visits" 
          domain="[('current_location_id', '=', context.get('user_current_location_id'))]"/> 


the visit action 
    <record id="visit_visit_act" model="ir.actions.server"> 
     <field name="name">Visit</field> 
     <field name="model_id" ref="model_visit_visit"/> 
     <field name="state">code</field> 
     <field name="code"> 
       action = env['visit.visit'].vsit_action() 


     </field> 
    </record> 

的default_search_current_id工作正常,但過濾filter_my_visits不過濾的觀點current_location_id =沒有,而其正確設置在背景 坦克

+0

我不明白你的過濾器。你想實現什麼?如果您想按給定位置過濾,只需將該字段放入搜索視圖。例如:在客戶樹中,您可以搜索標籤,但不必鍵入searchterm並按下回車鍵(快速搜索),您必須鍵入searchterm,選擇要搜索的「字段」(在我的示例標籤中;用鼠標或箭頭鍵),然後按回車。 – CZoellner

+0

我更新了問題,我想設置默認過濾器的問題,我不想給它靜態值既不希望用戶輸入值,我想設置它的值爲user_current_location_id, –

回答

1

它應該有可能與一點點的解決方法。不要使用ir.actions.act_window嘗試使用應該返回窗口操作的ir.actions.server。 爲什麼?因爲您需要將用戶的位置ID置入該窗口操作的上下文中。我只能通過使用帶有代碼的服務器動作來看到這種可能性。

只需填寫上下文與位置標識的窗口作用:

ctx = dict(env.context, 
    {'user_location_id': env.user.current_location_id.id, 
    'search_default_filter_my_visits': 1} 
return { 
    'name': 'Visit', 
    'view_type': 'form', 
    'res_model': 'visit.visit', 
    'type': 'ir.actions.act_window', 
    'context': ctx, 
    'search_view_id': env.ref('my_module.view_visit_filter'); 
} 

併爲您的過濾器:

<filter string="My visits" name="filter_my_visits" 
    domain="[('current_location_id', '=', context.get('user_location_id')]"/> 
+0

greate ... so我在哪裏編寫Python代碼?! –

+0

小提示:嘗試通過'ir.actions.server' ;-) - > [示例](https://github.com/odoo/odoo/blob/10)搜索Odoo的xml代碼。0/addons/crm/views/crm_lead_views.xml#L673) – CZoellner

+0

非常感謝你CZoellner ..我編輯了這個問題,以得出我設法做的事情 –