How to compute the value for default value in object fields in Odoo 8 models.py
We can't use the _default
attribute anymore in Odoo 8.
field_name = fields.datatype(
string=’value’,
default=compute_default_value
)
In the above field declaration, I want to call a method to assign default value for that field. For example:
name = fields.Char(
string='Name',
default= _get_name()
)
If you want to set some other fields as default, First of all, what you have to do is select the required values in the field, then go the set default option in the lady debugger button in the developer mode. Then you can see the field and field value in it.
You can set the default values for variables by adding ! default flag to the end of the variable value. It will not re-assign the value, if it is already assigned to the variable.
Log out the current user and log in the next user and continue the above process. whenever you sets a default value in this method,it stores in the Settings/Technical. Actions/User-defined defaults with the field name and model name. you can delete it there .
default_get method returns default values for the fields in fields_list. In this method you can assign desire value for the fields as defaults.
You can use a lambda function like this:
name = fields.Char(
string='Name',
default=lambda self: self._get_default_name(),
)
@api.model
def _get_default_name(self):
return "test"
A simpler version for the @ChesuCR answer:
def _get_default_name(self):
return "test"
name = fields.Char(
string='Name',
default=_get_default_name,
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With