How to filter several conditions using: action window domain

PURPOSE:

To filter records that match multiple conditions at the same time and present them in a tree view.

The context:

By understanding how to use action window domains, in a module we named “incident” has records that are categorized into States, Processes, User.

  • The states are: Request, Draft, Waiting, Assigned, Running,

  • In the “res.user” model we added the field “process” which assigns a Tryton user.

Currently, the window domains correctly filter requests by status. (only one condition)

DIFFICULTY:

The following example code runs without generating errors, however, when including several conditions we are unsuccessful, it does not do the expected filtering of the domain by the conditions.

ENVIRONMENT:

  • OS: Ubuntu 20.04.1 LTS
  • Python: 3.8.5
  • Trytond: 5.8.7
  • Tryton: 5.8.7
  • Module: 5.8
  • Postgresql: 12.5

EXAMPLE CODE:

Code to filter one condition

Code to filter by state:


<record model="ir.action.act_window.domain" id="act_incident_requested_list_domain_discarded">
<field name="name">Discarded</field>
<field name="sequence" eval="10"/>
<field name="domain" eval="[('state', '=', 'discarded')]" pyson="1"/>
<field name="count" eval="True"/>
<field name="act_window" ref="act_incident_requested_list"/>
</record>

Code to filter several conditions

Filtering by status and process of the active user.


<record model="ir.action.act_window.domain" id="act_incident_requested_list_domain_discarded">
<field name="name">Discarded</field>
<field name="sequence" eval="10"/>
<field name="domain" eval="[('state', '=', 'discarded'), ('process', '=', Eval('user', {}).get('process', None))]" pyson="1"/>
<field name="count" eval="True"/>
<field name="act_window" ref="act_incident_requested_list"/>
</record>

We appreciate your help !

You can not access the user attribute from a PYSON evaluation on the client side.
You have only access to _user which is the id of the logged user.
If you really need the value of the field process on the User model, you should consider adding it to the context by adding it to User._context_fields.

Thanks Cédric!

The process field is included within the context of the “res.user” model, i.e. in the User._context_fields.

We still fail to achieve domain filtering by querying several conditions at once.

We tried to implement other code variants in the domain, still, we do not get the results.
We will explain our code better, and we appreciate more guidance:

Our module has the following fields, added to the User._context_fields:

  • State
  • Assign_process
  • Assigned_agent
  • Applicant

In a menu, a window action is performed with domains, and works with the values contained in the State field: Request, Draft, Waiting, Assigned, Running.
It is intended to implement in a single code statement, in the same domain of the state, apply filters with multiple conditions, such as:

a- All records that meet a certain State and are assigned to the active user’s process (assign_process field).
b- All records that meet a certain State and are assigned to the active user (assigned_agent field).
c- All records that meet any State and were requested by the active user (Applicant field).
d- Other filters with similar queries…

When we refer to similar queries, what we want to achieve is:

  • Filters that always include querying the State (essential).
  • Filters querying the Assign_process, Assigned_agent, Applicant, are optional queries, as they will be filters with the combination of the State condition with any of the three, or all three at the same time.

validated code:

<record model="ir.action.act_window.domain" id="act_incident_requested_list_domain_all">
           <field name="name">All</field>
           <field name="sequence" eval="10"/>
           <field name="domain" eval="[('state', '=', 'discarded'), ('assign_process', '=', Eval('process', {}).get('process', None))]" pyson="1"/>
           <field name="count" eval="True"/>
           <field name="act_window" ref="act_incident_requested_list"/>
       </record>

another validated code alternative:

          <field name="domain" eval="[('state', '=', 'discarded'),  ('assign_process', 'in', [Eval('context', {}).get('process', -1), None])]" pyson="1"/>

This last code only tries to filter by the State and the Assign_process, but the record count does not work. We also failed to implement it by adding the other two conditions.

Are we forgetting something in our code?

If this is not the right way, it will help a lot to receive a similar example code.

It’s a bit difficult to understand, but as I understand it correctly how Tryton works, the domain view is used to group data based on one column. So if you want to filter you have to:

  1. use record_rules
  2. use the domain on a ir.action.act_window which also needs an (extra) menu entry

A use case I’m working on right now is that users only can see records they are assigned to. Those records have also a field called state. In this case I created the domain view with the state, so the user see the records grouped by state and I used a record_rule to filter out only the records they are assigned to.
If the assignation is a list of users, and the user is also added to another Many2One field in the model, you can filter down further with the second point, adding a new menu entry with ‘My …’. That domain will filter down on the user.

I think you have to split things and make three menu entries:

  1. Process
  2. Agent
  3. Applicant

You can use that to filter your records and then use the domain view to group each based on the state.