Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default values with methods in Odoo?

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()
)
like image 242
Jay Venkat Avatar asked Jul 23 '15 09:07

Jay Venkat


People also ask

What are ways to give default value to field in Odoo?

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.

How can we set default value to the variable?

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.

How do I give a default value to a many2one field in Odoo?

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 .

What is default get in Odoo?

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.


2 Answers

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"
like image 129
ChesuCR Avatar answered Sep 24 '22 00:09

ChesuCR


A simpler version for the @ChesuCR answer:

def _get_default_name(self):
    return "test"

name = fields.Char(
    string='Name',
    default=_get_default_name,
)
like image 25
Daniel Reis Avatar answered Sep 24 '22 00:09

Daniel Reis