Domain is used in Odoo to select records from a Model (database table) – in many different places:

  • Menus / Windows Deportment
  • Form Views – to select records from a one2many or many2many
  • Record Rules
  • Filters (see below)

One fashion to see how information technology works is to create a new User-Defined Filter.

  • e.g. We want to meet orders from customers in California.

Create a user-divers filter

The simplest mode to do this is to create a filter from the "front-cease", equally explained hither.

Creating from the 'back-end'

Enable "programmer mode", navigate to Technical / User-defined Filters and click on "New":

Select a model (in this case Auction Club) and click on "Add together Filter".

Now we can select a field on sale.lodge. If it'southward a one2many field we can select fields on the Related model (database tabular array).

For this example, nosotros'll select "Land" from the Invoice address

We tin enter the name of the state:

[["partner_invoice_id.state_id","=","California"]]

You can re-create and paste this domain and utilize it elsewhere.

A slightly more circuitous example: all products that can be purchased or sold. This uses the logical OR symbol "|" :

["|",    ["purchase_ok","=",True],    ["sale_ok","=",True] ]

If in that location were three weather you need to take OR twice (if there are iv you need three ORs, and so on).

Other logical operators are:

  • '&' = AND (this is the default so it is optional)
  • '!' = Non

At that place are many other operators:

  • =, !=, >, >=, <, <=
  • like, not like, ilike, not ilike
  • in, non in

You lot can experiment with them in User-Divers Filters to go a better understanding of the syntax.

Examples

Select Purchase Orders but not RFQs:

[('state','not in',('draft','sent'))]

Multiple conditions

  •  Either
    • Tin exist Purchased AND Tin can be Sold
  • OR
    • Landed Cost AND Service Blazon
["|",    "&",      ["purchase_ok","=",True],      ["sale_ok","=",True],    "&",      ["landed_cost_ok","=",True],      ["blazon","=","service"] ]

As explained above, the OR symbol "|" and the AND symbol "&" come before the conditions. For both of them, one symbol tin be followed by two conditions.

Here'due south an instance (from a Tape Dominion for Sales Order lines) which has more than two conditions:

"['&', '&', ('state', 'in', ['auction', 'washed']),                
('is_service', '=', Truthful),
'|', ('project_id','!=', False),
('task_id','!=', False)
]"

It starts with ii ANDs considering there are three atmospheric condition:

  1. The SO line status is either sale or done.
  2. The And so line is for a service item (is_service =Truthful)
  3. Either the project or task is specified

As you will run into, this can become quite complex – but using Filters is a fairly easy way to effort different combinations and eventually you should understand it!

Comparison with other fields

Domain tin can used to compare two fields rather than a field and a constant. For example, the Record Rule for "Sales / Own Documents only":

['|',
    ('user_id','=',user.id),
    ('user_id','=',Simulated)]

Note that the first field name is in quotes, the second one is non.

The first comparison is whether the User ID on the sales social club is the current user. The second is whether in that location is a User ID on the sales order. And then this will be true if:

  • Sales order is for the current user OR
  • Sales social club has no assigned salesperson (user_id is blank)

The comparison tin be with a field in another Model:

                [('partner_id', '=', commercial_partner_id)]              

As mentioned above at that place are many other operators, for case:

                [ '|',
    ('company_id', '=', Simulated),
    ('company_id', 'in', company_ids)
]

This will select records with no company specified (significant they are shared) OR records for the current companies (selected in the multi-company widget as explained here)

                [('type', 'not in', ['sale', 'purchase', 'cash', 'bank'])]              

This will select records where the blazon is non any of those specified (this example is a filter on Business relationship Journals)

A problem entering domains

Note that there are some limitations with domains if you enter them through the user interface.

In the above examples:

  • Filters use a pop-up dialog box which makes it easier to enter domains, only at the cost of limiting the functionality that is available.
  • Domains for Record Rules are entered in raw grade, which is a little more difficult but allows more than options.

It is possible to change the widget in the XML.

This is an excerpt from the XML for Automated Actions (View Name is "Automations")

<field proper name="filter_pre_domain"
widget="domain"
         options="{'model': 'model_name', 'in_dialog': True}"
         attrs="{'invisible': [('trigger', 'not in',['on_write','on_create_or_write'])]}"/>

The change is uncomplicated: use the char_domain widget instead

Links

Odoo ORM