LEFT | RIGHT |
1 .. _topics-domain: | 1 .. _topics-domain: |
| 2 |
2 | 3 |
3 ====== | 4 ====== |
4 Domain | 5 Domain |
5 ====== | 6 ====== |
| 7 |
| 8 Domains_ represent a set of records. A domain is a list of none or |
| 9 more clauses. A clause is a condition, which results true or false. |
| 10 A record belongs to a domain, when the final result of the list of |
| 11 clauses is true. |
| 12 |
| 13 .. _Domains: http://en.wikipedia.org/wiki/Data_domain |
| 14 |
| 15 |
| 16 Syntax |
| 17 ====== |
| 18 |
| 19 The definition of a simple domain with one clause is represented |
| 20 by this pattern:: |
| 21 |
| 22 domain = [(<field name>, <operator>, <operand>)] |
| 23 |
| 24 ``<field name>`` |
| 25 Is the name of a :mod:`trytond.model.fields` or a |
| 26 :ref:`pyson <topics-pyson>` statement, that evaluates to a |
| 27 string. |
| 28 |
| 29 A field of type :class:`trytond.model.fields.Many2One` can be |
| 30 dereferenced to related models. This is illustrated by the |
| 31 following example:: |
| 32 |
| 33 domain = [('country.name', '=', 'Japan')] |
| 34 |
| 35 The number of *dots* in a clause is not limited. |
| 36 |
| 37 ``operator`` |
| 38 Is an operator out of `Domain Operators`_ or a |
| 39 :ref:`pyson <topics-pyson>` statement, that evaluates to |
| 40 a domain operator string. |
| 41 |
| 42 ``operand`` |
| 43 Is an operand or a :ref:`pyson <topics-pyson>` statement. The |
| 44 type of operand depends on the kind of <field name>. |
| 45 |
| 46 The definition of an empty domain is:: |
| 47 |
| 48 domain = [] |
| 49 |
| 50 An empty domain without clauses will always return all *active*· |
| 51 records. A record is active, when its appropriate |
| 52 :class:`~trytond.model.Model` contains a |
| 53 :class:`~trytond.model.fields.Boolean` field with name ``active``, |
| 54 and set to true. When the appropriate :class:`~trytond.model.Model` |
| 55 does not contain a :class:`~trytond.model.fields.Boolean` field with |
| 56 name ``active`` all records are returned. |
| 57 |
| 58 A domain can be setup as a combination of clauses, like shown in |
| 59 this pattern:: |
| 60 |
| 61 domain = [ |
| 62 ('field name1', 'operator1', 'operand1'), |
| 63 ('field name2', 'operator2', 'operand2'), |
| 64 ('field name3', 'operator3', 'operand3'),] |
| 65 |
| 66 The single clauses are implicitly combined with a logical |
| 67 AND_ operation. |
| 68 |
| 69 |
| 70 In the domain syntax it is possible to provide explicitly the |
| 71 combination operation of the clauses. These operations can be AND_ |
| 72 or OR_. This is illustrated by the following pattern:: |
| 73 |
| 74 domain = [ 'OR', [ |
| 75 ('field name1', 'operator1', 'operand1'), |
| 76 ('field name2', 'operator2', 'operand2'), |
| 77 ], [ |
| 78 ('field name3', 'operator3', 'operand3'), |
| 79 ],] |
| 80 |
| 81 .. _AND: http://en.wikipedia.org/wiki/Logical_and |
| 82 .. _OR: http://en.wikipedia.org/wiki/Logical_or |
| 83 |
| 84 |
| 85 Here the domain is evaluated like this: ``((clause1 AND clause2) |
| 86 OR clause3)``. Please note that the ``AND`` operation is implicit |
| 87 assumed when no operator is given. While the ``OR`` operation must |
| 88 be given explicitly. The former pattern has the same result as the |
| 89 following completely explicit domain definition:: |
| 90 |
| 91 domain = [ 'OR', |
| 92 [ 'AND', [ |
| 93 ('field name1', 'operator1', 'operand1'), |
| 94 ], [ |
| 95 ('field name2', 'operator2', 'operand2'), |
| 96 ], |
| 97 ], [ |
| 98 ('field name3', 'operator3', 'operand3'), |
| 99 ],] |
| 100 |
| 101 Obviously the use of the implicit ``AND`` operation makes the code |
| 102 more readable. |
| 103 |
| 104 |
| 105 Domain Operators |
| 106 ================ |
| 107 |
| 108 The following operators are allowed in the domain syntax. |
| 109 ``<field name>``, ``<operator>`` and ``<operand>`` are dereferenced |
| 110 to their values. The description of each operator follows this |
| 111 pattern, unless otherwise noted:: |
| 112 |
| 113 result := <field value> <operator> <other value> |
| 114 |
| 115 ``=`` |
| 116 ----- |
| 117 |
| 118 Is a parity operator. Results true when ``<field value>`` |
| 119 equals to ``<other value>``. |
| 120 |
| 121 ``!=`` |
| 122 ------ |
| 123 |
| 124 Is an imparity operator. It is the negation of the `=`_ operator. |
| 125 |
| 126 ``like`` |
| 127 -------- |
| 128 |
| 129 Is a pattern matching operator. Results true when ``<field value>`` |
| 130 is contained in the pattern represented by ``<other value>``. |
| 131 |
| 132 In ``<other value>`` an underscore (``_``) matches any single |
| 133 character, a percent sign (``%``) matches any string with zero |
| 134 or more characters. To use ``_`` or ``%`` as literal, use the |
| 135 backslash ``\`` to escape them. All matching is case sensitive. |
| 136 |
| 137 ``not like`` |
| 138 ------------ |
| 139 |
| 140 Is a pattern matching operator. It is the negation of the `like`_ |
| 141 operator. |
| 142 |
| 143 ``ilike`` |
| 144 --------- |
| 145 |
| 146 Is a pattern matching operator. The same use as `like`_ operator, |
| 147 but matching is case insensitive. |
| 148 |
| 149 ``not ilike`` |
| 150 ------------- |
| 151 |
| 152 Is a pattern matching operator. The negation of the `ilike`_ operator. |
| 153 |
| 154 ``in`` |
| 155 ------ |
| 156 |
| 157 Is a list member operator. Results true when ``<field value>`` is |
| 158 in ``<other value>`` list. |
| 159 |
| 160 ``not in`` |
| 161 ---------- |
| 162 |
| 163 Is a list non-member operator. The negation of the `in`_ operator. |
| 164 |
| 165 ``<`` |
| 166 ----- |
| 167 |
| 168 Is a *less than* operator. The result is true for type string of |
| 169 ``<field value>`` when ``<field value>`` is alphabetically |
| 170 sorted before ``<other value>``. |
| 171 |
| 172 The result is true for type number of ``<field value>`` when |
| 173 ``<field value>`` is less than ``<other value>``. |
| 174 |
| 175 ``>`` |
| 176 ----- |
| 177 |
| 178 Is a *greater than* operator. The result is true for type string of |
| 179 ``<field value>`` when ``<field value>`` is alphabetically |
| 180 sorted after ``<other value>``. |
| 181 |
| 182 The result is true for type number of ``<field value>`` when |
| 183 ``<field value>`` is greater ``<other value>``. |
| 184 |
| 185 ``<=`` |
| 186 ------ |
| 187 |
| 188 Is a *less than or equal* operator. Results the same as using the |
| 189 `<`_ operator, but also returns true when ``<field value>`` is |
| 190 equal to ``<other value>``. |
| 191 |
| 192 ``>=`` |
| 193 ------ |
| 194 |
| 195 Is a *greater than or equal* operator. Results the same as using |
| 196 the `>`_ operator, but also returns true when ``<field value>`` |
| 197 is equal to ``<other value>``. |
| 198 |
| 199 ``child_of`` |
| 200 ------------ |
| 201 |
| 202 Is a parent child comparison operator. When ``<field value>`` is a |
| 203 :class:`~trytond.model.fields.one2many` results true when |
| 204 ``<field value>`` is a child of ``<other value>``. ``<field value>`` |
| 205 and ``<other value>`` are represented each by an ``id``. |
| 206 When ``<field value>`` is a :class:`~trytond.model.fields.many2many` |
| 207 not linked to itself, the clause pattern changes to:: |
| 208 |
| 209 (<field value>, <operator>, <value>, <parent field>) |
| 210 |
| 211 Where ``<parent field>`` is the name of the field on the target |
| 212 model that is the :class:`~trytond.model.fields.many2one` to parent. |
| 213 |
| 214 ``not child_of`` |
| 215 ---------------- |
| 216 |
| 217 Is a parent child comparison operator. It is the negation of the |
| 218 `child_of`_ operator. |
| 219 |
| 220 |
LEFT | RIGHT |