Community mailing list archives

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

Pedro Cachaca
- 03/24/2016 10:35:29
I finally found a way to come over the same uuid applied to all the existing records, when adding below new field with default method to a module.
access_token = fields.Char(default=lambda self: self._default_access_token())

I call a function "replace_not_unique_token" during module update, and it is done ;)

Below is the code.

Note : "post_init_hook" is not a solution because seems to be called only during first install of the module.
Note : Extending "_set_default_value_on_column" method is a little intrusive I think...

<function model="" name="replace_not_unique_token"/>

class channel(models.Model):

    access_token = fields.Char(default=lambda self: self._default_access_token())

    def _default_access_token(self):
        return '%s' % uuid.uuid4().hex

    def replace_not_unique_token(self):
        """Method called during module update to remove not unique token
        created by default method when add new token field to module.
        because when a new field is added to the Odoo datamodel, and it has
        a default method, this method is only called once and the result is
        applied to all existing records."""'Replace not unique token... START')
        Channel = self.env['']
        channels =[])
        previous_token = ''
        for channel in channels.sorted(key=lambda r: r.access_token):
            token = channel.access_token
            if token == previous_token:
      'Replace not unique token...')
                    'access_token': Channel._default_access_token(),
            previous_token = token'Replace not unique token... END')


On Thu, Mar 24, 2016 at 12:57 PM, Ondrej Kuznik <> wrote:
On 24/03/16 11:23, pedro cachaca wrote:
> Hi Stefan and Ondrej,
> I am trying  with "post_init_hook" on Odoo 8 without success,
> I follow this example
> :
> Is "post_init_hook" executed only at installation or -i ? or is
> *available for module update -u ?*
> Ondrej, is _set_default_value_on_column another way to do ? not easy to
> find example...

Hi Pedro,
default function and _set_default_value_on_column are only called when
the column does not exist yet. If you have already tried loading the
module, upgrading it will not trigger it again. So long as you are
developing your module on a test system, just drop the column and
upgrade the module.

As far as I am aware, there are no decent public examples of this
function being used. There was one in an earlier pull request of ours:


credativ Ltd
Suite 5, Bloxam Court
Corporation Street           UK office:  +44 1788 298150
Rugby                        Email:
CV21 2DU                     Web:

We would love to hear you your feedback on the service provided. If
you have anything you would like to contact us on the service we have
provided, please send your feedback to

credativ Ltd is registered in England & Wales, company no. 5261743
Certified by CompTIA / AccredIT UK with the ICT Supply standard of
quality for Software Product Design and Development

Post to: