Browse Source

[IMP] - get default values for non record onchange call

other improvement:

set onchange_specs to all fields as _onchange_spec()
retrun onchange fields for default view
return field value if it's set in onchange_fields (usuful to get default value for a field)
pull/1461/head
sbejaoui 6 years ago
parent
commit
0f4f8bbd74
  1. 11
      onchange_helper/models/base.py
  2. 1
      onchange_helper/readme/CONTRIBUTORS.rst
  3. 11
      onchange_helper/readme/USAGE.rst

11
onchange_helper/models/base.py

@ -22,9 +22,12 @@ class Base(models.AbstractModel):
@api.model
def play_onchanges(self, values, onchange_fields):
onchange_specs = self._onchange_spec()
# _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()
}
all_values = values.copy()
# If self is a record (play onchange on existing record)
# we take the value of the field
@ -38,7 +41,8 @@ class Base(models.AbstractModel):
}
)
else:
record_values = {}
# We get default values, they may be used in onchange
record_values = self.default_get(self._fields.keys())
for field in self._fields:
if field not in all_values:
all_values[field] = record_values.get(field, False)
@ -52,5 +56,6 @@ class Base(models.AbstractModel):
return {
f: v
for f, v in all_values.items()
if not self._fields[f].compute and (f in values or f in new_values)
if not self._fields[f].compute
and (f in values or f in new_values or f in onchange_fields)
}

1
onchange_helper/readme/CONTRIBUTORS.rst

@ -1,3 +1,4 @@
* Guewen Baconnier <guewen.baconnier@camptocamp.com>
* Florian da Costa <florian.dacosta@akretion.com>
* Andrea Stirpe <a.stirpe@onestein.nl>
* Souheil Bejaoui <souheil.bejaoui@acsone.eu>

11
onchange_helper/readme/USAGE.rst

@ -11,6 +11,17 @@ Example if you want to create a sale order and you want to get the values relati
Then, `vals` will be updated with partner_invoice_id, partner_shipping_id, pricelist_id, etc...
Default values will be used to process onchange methods, if respective fields are not set in `vals`.
You can get them if you pass fields name in the list of fields.
`vals = {'partner_id': 1}`
`vals = self.env['sale.order'].play_onchanges(vals, ['partner_id', 'date_order'])`
`vals` will contain, in addition to the changed values, the default value for `date_order`
You can also use it on existing record for example:
`vals = {'partner_shipping_id': 1}`

Loading…
Cancel
Save