diff options
| author | Robot Odoo <xmo+roboto@odoo.com> | 2019-04-30 17:25:56 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-04-30 17:25:56 +0200 |
| commit | 97780e5b8bfe925e9f3908ef4b1925dfbbe1d08e (patch) | |
| tree | 6c62f3f2ba05952fe75eb87c4dbdb543d66a3e42 /odoo/fields.py | |
| parent | 0d21735a92c8d0b60babaac6d62dbc8eda7c11ea (diff) | |
| parent | f44571f550dfa34ddb1a62bbe03e78a4dadf8eda (diff) | |
[MERGE] fields: relational fields automatically behave as mapped
In other words, `recs.foo_id == recs.mapped('foo_id')`.
closes odoo/odoo#32897
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
Diffstat (limited to 'odoo/fields.py')
| -rw-r--r-- | odoo/fields.py | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/odoo/fields.py b/odoo/fields.py index 5d0cf903232..00a204c9404 100644 --- a/odoo/fields.py +++ b/odoo/fields.py @@ -989,7 +989,7 @@ class Field(MetaField('DummyField', (object,), {})): if record is None: return self # the field is accessed through the owner class - if record: + if record._ids: # only a single record may be accessed record.ensure_one() try: @@ -2033,6 +2033,15 @@ class _Relational(Field): 'context': {}, # context for searching values } + def __get__(self, records, owner): + # base case: do the regular access + if records is None or len(records._ids) <= 1: + return super().__get__(records, owner) + # multirecord case: return the union of the values of 'self' on records + get = super().__get__ + comodel = records.env[self.comodel_name] + return comodel.union(*[get(record, owner) for record in records]) + def _setup_regular_base(self, model): super(_Relational, self)._setup_regular_base(model) if self.comodel_name not in model.pool: @@ -2356,7 +2365,7 @@ class _RelationalMulti(_Relational): super(_RelationalMulti, self)._compute_related(records) if self.related_sudo: # determine which records in the relation are actually accessible - target = records.mapped(self.name) + target = records[self.name] target_ids = set(target.search([('id', 'in', target.ids)]).ids) accessible = lambda target: target.id in target_ids # filter values to keep the accessible records only |
