Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] onchange_helper: Do not overwrite precomputed fields #3152

Open
wants to merge 2 commits into
base: 16.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions onchange_helper/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ def _get_new_values(self, record, on_change_result):
return new_values

@api.model
def play_onchanges(self, values, onchange_fields):
def play_onchanges(self, values, onchange_fields, view_info=None):
"""
:param values: dict of input value that
:param onchange_fields: fields for which onchange methods will be
played
played
:param view_info: view containing the fields that should change,
as returned by `get_view`

Order in onchange_fields is very important as onchanges methods will
be played in that order.
:return: changed values
"""
# _onchange_spec() will return onchange fields from the default view
# we need all fields in the dict even the empty ones
# otherwise 'onchange()' will not apply changes to them
onchange_specs = {field_name: "1" for field_name, field in self._fields.items()}
onchange_specs = self._onchange_spec(view_info=view_info)
all_values = values.copy()
# If self is a record (play onchange on existing record)
# we take the value of the field
Expand All @@ -49,10 +49,21 @@ def play_onchanges(self, values, onchange_fields):
else:
# We get default values, they may be used in onchange
record_values = self.default_get(self._fields.keys())
# we need all fields in the dict even the empty ones
# otherwise 'onchange()' will not apply changes to them
for field in self._fields:
if field not in all_values:
all_values[field] = record_values.get(field, False)

if not self:
# Precomputed fields will be computed just before creation
# and should not be overwritten by changed values
all_values = {
field_name: all_values[field_name]
for field_name in all_values
if not self._fields[field_name].precompute
}

new_values = {}
for field in onchange_fields:
onchange_values = self.onchange(all_values, field, onchange_specs)
Expand Down
Loading