1

We have an instance of V9 odoo running. Anywhere that a domain filter is used with an evaluated value, an error is thrown.

As an example, on the res.users searchview I have created a simple domain filter:

[('id', '=', user.id)]

When applying this filter the following error is thrown:

Error: Failed to evaluate search criterions: {"code":400,"message":"Evaluation Error","data":{"type":"local_exception","debug":"Local evaluation failure\nNameError: name 'user' is not defined\n\n{\"domains\":[[],\"['id', '=', user.id]\"],\"contexts\":[{\"lang\":\"en_GB\",\"tz\":\"Asia/Saigon\",\"uid\":566,\"params\":{\"action\":69,\"page\":0,\"limit\":80,\"view_type\":\"list\",\"model\":\"res.users\",\"menu_id\":79,\"_push_me\":false},\"search_default_no_share\":1},{},\"{}\"],\"group_by_seq\":[\"{}\"]}"}}

This occurs no matter what odoo system values are used. For example:

  • user.partner_id
  • user.name
  • user.id

The only one that does not through an error is uid, i.e.

[('id', '=', uid)]

The purpose of accessing user is to access further values related to the current user. The entire code for the domain filter I am trying to create is the following:

<record id="crm_opportunity_search_view" model="ir.ui.view">
  <field name="name">crm.opportunity.search.view</field>
  <field name="model">crm.opportunity</field>
  <field name="arch" type="xml">
    <search string="Opportunities">
      <field name="name" filter_domain="[('name','ilike',self)]"/>
      <filter string="My Division" name="my_division" domain="[('owner_id.business_unit_id.id', '=', user.partner_id.business_unit_id.id)]"/>
    </search>
  </field>
</record>

"My division" is an available filter in the filters menu from opportunities. However, when selected throws an error that "user" is not defined.

I have tried adding the domain filter in XML and using the advanced filters in the technical settings to no avail.

I have tried this in two separate v9 instances with the same result.

Trying to add any domain filter in a new instance of v11 such as below, using user.id or uid returns a "domain filter not properly formed" error.

[["name","=",user.id]]

Any clues on what I am doing wrong would be welcomed.

forvas
  • 9,801
  • 7
  • 62
  • 158
3piece
  • 178
  • 3
  • 12
  • Domains are list of tuples, on previous versions used to be like this: `` – ChesuCR Mar 01 '18 at 19:12
  • Thanks for the response @ChesuCR, using the following filter_domain:[('id', '=', user.id)] also fails in v9. – 3piece Mar 01 '18 at 23:40
  • However, I need to access/evaluate another related value of the current user "user.partner_id.business_unit_id.id" which is a related field we have added on the res.partner form. – 3piece Mar 01 '18 at 23:49

1 Answers1

2

The problem is that user is a variable which unfortunately is only available when writing records from specific models, like for example ir.rule (here you can use user in the domain_force field).

So that variable doesn't exist in a search view, that's why you get the error.

Take a look at the Rules official documentation: https://www.odoo.com/documentation/8.0/reference/security.html

A domain used to check whether a given record matches the rule (and is accessible) or does not (and is not accessible). The domain is evaluated with two variables in context: user is the current user's record and time is the time module

So the solution you're looking for is this one:

Create a new computed field named my_division in crm.opportunity model:

@api.multi
@api.depends('owner_id', 'owner_id.business_unit_id')
def _compute_my_division(self):
    for opportunity in self:
        if opportunity.owner_id.business_unit_id.id == self.env.user.partner_id.business_unit_id.id:
            opportunity.my_division = True

my_division = fields.Boolean(
    compute='_compute_my_division',
    string='My division',
    store=True,
)

Add this field (invisible) to the views (tree, kanban, etc) you can search for. Then modify your search filter this way:

<filter string="My Division" name="my_division" domain="[('my_division','=',1)]"/>

That should work. Let me know.

EDIT

Sometimes, when you create a computed field which is stored in the database, it doesn't behave as expected (it stops recalculating itself). When I'm fed up with struggling with that, I do the following trick (I don't like it at all but... I need to carry on).

You can preserve the filter I wrote you above, but you have to modify a couple of things in the crm.opportunity model:

First, make my_division a non-computed field. Then, modify crm.opportunity create and write ORM methods (be careful with the super -don't write exactly CrmOpportunity, write the name you chose for the Python class-):

my_division = fields.Boolean(
    string='My division',
    default=False,
)

@api.model
def create(self, vals)
    opportunity = super(CrmOpportunity, self).create(vals)
    if opportunity.owner_id.business_unit_id.id == self.env.user.partner_id.business_unit_id.id:
        opportunity.write({
            'my_division': True,
        })
    return opportunity

@api.multi
def write(self, vals)
    update = super(CrmOpportunity, self).write(vals)
    for opportunity in self:
        if opportunity.owner_id.business_unit_id.id == self.env.user.partner_id.business_unit_id.id and \
           opportunity.my_division is False:
            opportunity.write({
                'my_division': True,
            })
        elif opportunity.owner_id.business_unit_id.id != self.env.user.partner_id.business_unit_id.id and \
           opportunity.my_division is True:
            opportunity.write({
                'my_division': False,
            })
        else:
            continue
    return update

This must work for sure, but it's not very clean.

forvas
  • 9,801
  • 7
  • 62
  • 158
  • Thanks @forvas, that would explain things. So any ideas on how people workaround this to have filters that are based on the current users related fields? – 3piece Mar 02 '18 at 09:34
  • I've edited my answer to solve your problem, take a look. – forvas Mar 02 '18 at 10:47
  • I tried your code and it mostly works thanks, as it creates a calculated field and there is a hidden column on the tree view. The strange thing is that although the logic looks correct, there are opportunities from different business_units being displayed. Trying to debug it at the moment with not much luck. Users with different divisions, seem to see the same records which is 687. – 3piece Mar 05 '18 at 21:34
  • I think it is not re-calculating. I placed a debug line, when I remove the store=True value, it tries to calculate, but also throw an error that to search a computed field it must be stored. When I change the users Division, they still see the same 687 records. – 3piece Mar 05 '18 at 21:45
  • @3piece Sometimes computed fields which are stored in the database don't recalculate, it's an incomprehensible error in Odoo... Try to create a new opportunity and check if it works (don't try with the ones created before the changes in your code). – forvas Mar 06 '18 at 09:22
  • I've edited my answer to solve your problem with a workaround. – forvas Mar 06 '18 at 12:20