Community mailing list archives

community@mail.odoo.com

Re: how to set a unique default value for new fields

by
Pedro Cachaca
- 03/24/2016 04:29:00
Hi Pedro, unfortunately, even when adding below new field to a model and updating the module, the method is called only 1 time and all existing records received the same uuid ;(

def _default_token(self):
    return uuid.uuid4().hex

token = fields.Char(default=_default_token)

On Thu, Mar 24, 2016 at 2:10 AM, Pedro Manuel Baeza Romero <pedro.baeza@gmail.com> wrote:
This is because at the end, you're passing a static string to the lambda expression. You need to have a dynamic method that is called each time the default is called:

def _default_token(self):
    return uuid.uuid4().hex

token = fields.Char(default=_default_token)

Regards.

2016-03-24 0:32 GMT+01:00 pedro cachaca <pcachaca33@gmail.com>:
Hello,

I want to add following new field to a model :

token = fields.Char(default=lambda self: '%s' % uuid.uuid4().hex )

But when I update the module, all existing records get the same value uuid for token...

The lambda function is executed one time and result applied to all the records !

How to make the function executed once updating each records ?

Thanks.

_______________________________________________
Mailing-List: https://www.odoo.com/groups/community-59
Post to: mailto:community@mail.odoo.com
Unsubscribe: https://www.odoo.com/groups?unsubscribe


_______________________________________________
Mailing-List: https://www.odoo.com/groups/community-59
Post to: mailto:community@mail.odoo.com
Unsubscribe: https://www.odoo.com/groups?unsubscribe