markdown Odoo orm搜索
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown Odoo orm搜索相关的知识,希望对你有一定的参考价值。
### Odoo orm
Each tuple in the search domain needs to have 3 elements, in the form: `('field_name', 'operator', value)`, where:
### Fields
`field_name` must be a valid name of field of the object model, possibly following many-to-one relationships using dot-notation, e.g `'street'` or `'partner_id.country'` are valid values.
### Operators
The operator must be a string with a valid comparison operator from this list: `=, !=, >, >=, <, <=, like, ilike, in, not in, child_of, parent_left, parent_right`
The semantics of most of these operators are obvious. The child_of operator will look for records who are children or grand-children of a given record, according to the semantics of this model (i.e following the relationship field named by self._parent_name, by default parent_id.
value must be a valid value to compare with the values of field_name, depending on its type.
### Domain criteria
Domain criteria can be combined using 3 logical operators than can be added between tuples:
```
'&' (logical AND, default)
'|' (logical OR)
'!' (logical NOT)
```
These are prefix operators and the arity of the `'&'` and `'|'` operator is 2, while the arity of the `'!'` is just 1. Be very careful about this when you combine them the first time.
### Example
Here is an example of searching for Partners named `ABC` from `Belgium` and `Germany` whose language is __not__ `english` ::
```
[('name','=','ABC'),'!',('language.code','=','en_US'),'|',
('country_id.code','=','be'),('country_id.code','=','de')]
```
The `'&'` is omitted as it is the default, and of course we could have used `'!='` for the language, but what this domain really represents is::
```
[(name is 'ABC' AND (language is NOT english) AND (country is Belgium OR Germany))]
```
#### Model example
```
result = self.env['res.model'].search(['&', ('value', '=', True), ('second_value', '=', True)])
```
#### Sort
```
result = self.env['res.model'].search([('value', '=', True)],order='value desc')
```
Available values are
* `desc`
* `asc`
#### Limit
```
result = self.env['res.model'].search([('value', '=', True)],limit=1)
```
#### Update many2many
Here's an example from the stock module:
invoice_line_id = invoice_line_obj.create(cursor, user, {
'name': name,
'origin': origin,
'invoice_id': invoice_id,
'uos_id': uos_id,
'product_id': move_line.product_id.id,
'account_id': account_id,
'price_unit': price_unit,
'discount': discount,
'quantity': move_line.product_uos_qty or move_line.product_qty,
'invoice_line_tax_id': [(6, 0, tax_ids)],
'account_analytic_id': account_analytic_id,
}, context=context)
self._invoice_line_hook(cursor, user, move_line, invoice_line_id)
The `invoice_line_tax_id` field is a many-to-many relationship, and the `(6, 0, tax_ids)` means to replace any existing records with those in `tax_ids`. Because you're calling create(), there's nothing to replace.
A full list of options is in the documentation for the osv class.
For a many2many field, a list of tuples is expected. Here is the list of tuple that are accepted, with the corresponding semantics
* `(0, 0, { values })` link to a new record that needs to be created with the given values dictionary
* `(1, ID, { values })` update the linked record with id = ID (write values on it)
* `(2, ID)` remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)
* `(3, ID)` cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)
* `(4, ID)` link to existing record with id = ID (adds a relationship)
* `(5)` unlink all (like using (3,ID) for all linked records)
* `(6, 0, [IDs])` replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)
以上是关于markdown Odoo orm搜索的主要内容,如果未能解决你的问题,请参考以下文章