diff options
| author | Raphael Collet <rco@odoo.com> | 2020-06-09 12:08:09 +0000 |
|---|---|---|
| committer | Raphael Collet <rco@odoo.com> | 2020-08-20 13:39:04 +0000 |
| commit | 488e334fc8fd6601e304635fa577c98b9bfd18e6 (patch) | |
| tree | 30c70f257a17ccf36a71ffa66dd917ddf9ef978e /odoo/models.py | |
| parent | 540682b9bef4d3ee1c02056aa5199b5b463a9363 (diff) | |
[REF] core: combine default_get() with first onchange()
When creating a new record, the client calls `default_get()`, completes
the returned values with `False`, and calls `onchange()` to apply the
onchange to the defaults.
Optimize the double round-trip by integrating `default_get()` inside
`onchange()` for the first call. The method is called with an empty
list of fields, and usually no field values, except for records in a
one2many field. In this case, `onchange()` does both steps above.
Task 2261084
Diffstat (limited to 'odoo/models.py')
| -rw-r--r-- | odoo/models.py | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/odoo/models.py b/odoo/models.py index d82806b6862..f3a2b4d7530 100644 --- a/odoo/models.py +++ b/odoo/models.py @@ -5857,6 +5857,10 @@ Fields: names (in view order), or False :param field_onchange: dictionary mapping field names to their on_change attribute + + When ``field_name`` is falsy, the method first adds default values + to ``values``, applies onchange methods to them, and return all the + fields in ``field_onchange``. """ # this is for tests using `Form` self.flush() @@ -5869,6 +5873,8 @@ Fields: else: names = [] + first_call = not names + if not all(name in self._fields for name in names): return {} @@ -5931,14 +5937,16 @@ Fields: ) ) - def diff(self, other): + def diff(self, other, force=False): """ Return the values in ``self`` that differ from ``other``. Requires record cache invalidation for correct output! """ record = self['<record>'] result = {} for name, subnames in self['<tree>'].items(): - if (name == 'id') or (other.get(name) == self[name]): + if name == 'id': + continue + if not force and other.get(name) == self[name]: continue field = record._fields[name] if field.type not in ('one2many', 'many2many'): @@ -5968,6 +5976,16 @@ Fields: nametree = PrefixTree(self.browse(), field_onchange) + if first_call: + names = list(nametree) + values.update(self.default_get([ + name + for name in names + if name not in values + ])) + for name in names: + values.setdefault(name, False) + # prefetch x2many lines without data (for the initial snapshot) for name, subnames in nametree.items(): if subnames and values.get(name): @@ -6065,7 +6083,7 @@ Fields: # determine values that have changed by comparing snapshots self.invalidate_cache() - result['value'] = snapshot1.diff(snapshot0) + result['value'] = snapshot1.diff(snapshot0, force=first_call) # format warnings warnings = result.pop('warnings') |
