Skip to content

Instantly share code, notes, and snippets.

@lepistone
Created March 3, 2015 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lepistone/3ca65107fc7344440777 to your computer and use it in GitHub Desktop.
Save lepistone/3ca65107fc7344440777 to your computer and use it in GitHub Desktop.
How to create and pre-populate a not null column in odoo.
def _auto_init(self, cr, context):
"""Fill in the required consignee column with default values.
This is similar to the solution used in mail_alias.py in the core.
The installation of the module will succeed with no errors, and the
column will be required immediately (the previous solution made it
required only on the first module update after installation).
"""
# create the column non required
self._columns['consignee_id'].required = False
super(SaleOrder, self)._auto_init(cr, context=context)
# fill in the empty records
no_consignee_ids = self.search(cr, SUPERUSER_ID, [
('consignee_id', '=', False)
], context=context)
self.write(cr, SUPERUSER_ID, no_consignee_ids,
{'consignee_id': SUPERUSER_ID}, context)
# make the column required again
self._columns['consignee_id'].required = True
super(SaleOrder, self)._auto_init(cr, context=context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment