2
votes

My question is:

How can I reference a field in res.user and use it as a custom filter when opening a tree view in Odoo 10?

I extended res.user to add a field called: assigned_office. This is a Selection field.

I am now trying to use this value to filtering records when calling a specific window action to filter records by default.

I have tried adding the following to my Window Action, but it gave me an error (NameError: name 'user' not defined).

<field name="domain">[('responsible_office','=',user.assigned_office)]</field>    

I have tried to modify the search view by adding variations of this to the existing filter:

('responsible_office','=',user.assigned_office)
('responsible_office','=',ref='self.assigned_office))
('responsible_office','=',ref=assigned_office)

But I am also getting errors:

Error: Failed to evaluate search criterions: {"code":400,"message":"Evaluation Error","data":{"type":"local_exception","debug":"Local evaluation failure\nExpected \")\", got \"(name)\"\n\n{\"domains\":[[],\"[
('expiration_date','<=',(context_today())
,('responsible_org','=',ref='self['assigned_org']')
]\"],\"contexts\":[{\"lang\":\"en_US\",\"tz\":false,\"uid\":1,\"search_default_weekly_expirees_filter\":1,\"params\":{\"action\":205}},{}],\"group_by_seq\":[]}"}}

I am obviously doing something wrong and I hope there is a simple answer.

Here is my code:

Window Action:

<record id="my_tool_weekly_expirees_action" model="ir.actions.act_window" >              
  <field name="name">Weekly Expirees</field>
  <field name="res_model">my_tool.member</field>
  <field name="view_type">form</field>
  <field name="view_mode">tree,form</field>
  <field name="context">{"search_default_weekly_expirees_filter":1}</field>  
  <field name="view_id" ref="weekly_expirees_view"/>         
</record>

SEARCH:

<record id="members_search_view" model="ir.ui.view" >
  <field name="name">Search</field>
  <field name="model">my_tool.member</field>
  <field name="arch" type="xml">
      <search>
          <field name="first_name"/> 
          <field name="last_name"/>

          <field name="expiration_date" string="weekly_expirees" filter_domain="[('expiration_date','&lt;=',(context_today())]"/>  
          <separator/>
          <filter string="Weekly Expirees" name="weekly_expirees_filter" domain="[('expiration_date','&lt;=',(context_today())]" help="..."/>
          <separator/>             

          <field name="expiration_date"/>
          <field name="responsible_office"/>
      </search>
  </field>      
</record>

TREE VIEW

<record id="members_list_view" model="ir.ui.view" >
  <field name="name">members.list</field>
  <field name="model">my_tool.member</field>
  <field name="arch" type="xml">       
      <tree>          
      <field name="first_name"/>
      <field name="middle_name"/>
      <field name="last_name"/>             
      <field name="expiration_date"/> 
      <field name="responsible_office"/> 
      <field name="country"/>  
    </tree>
  </field>
</record>

I have searched stackoverflow.com and the forums at odoo.com and found complicated approaches that I either do not understand or want to do:

The most promising is, but it still seems to be a hack work around:

Odoo v9 domain filter with value user.id throws error that user is not defined

Another one that I found that might be even better is.

how to filter tree view with dynamic field odoo-10

1

1 Answers

2
votes

The most 2 simple ways to get your domain working will be:

  • Create a Security ir.rule record for the model where you will be able to use the variable user to refer to the current user in the domain_force field, like:

    <record id="my_tool_member_rule" model="ir.rule">
        <field name="name">My Tool Members Rule</field>
        <field name="model_id" ref="my_tool.member"/>
        <field name="domain_force">[('responsible_office','=',user.assigned_office)]</field>
        <field name="perm_read" eval="True"/>
        <field name="perm_create" eval="False"/>
        <field name="perm_write" eval="False"/>
        <field name="perm_unlink" eval="False"/>
    </record>
    
  • Override the model my_tool.member search method to add the custom domain, like:

from odoo import api, models class MyToolMember(models.Model): _name = 'my_tool.member'

    def search(self, args, offset=0, limit=None, order=None, count=False):
        args.append(('responsible_office', '=', self.env.user.assigned_office))
        return super(MyToolMember, self).search(args, offset=0, limit=None, order=order, count=False)