Odoo Help

2

Is there a way to set a value for a field for all existing records in the database at addon installation only?

By
Michael Karrer
on 8/11/15, 8:44 AM 1,571 views

Situation:

  1. I have an existing Database with the model event.calendar.

  2. In this Database there are already a lot of records for calendar.event

  3. I install a new addon that extends the event.calendar model with a new field "category" with required=TRUE

RESULT / ERROR:

Since the existing records do not hold any value for the new field the installation of the addon fails. Because the NOT NULL constrain could not be created since there are already records with NULL values for the new field. (Chicken Egg problem in some way)

QUESTION:

Is there a way to set a default value for this new field but just for all existing records in the Database at module (addon) installation?


Hint:

I do not want to set a permanent _default parameter for this field since after the installation is done the user should carefully set this field to a meaningful value.

4

Hi,

To achieve your goal you need to define one method in your class where you add that new field. And set data into existing records. Please have a look on the below code.

class calendar_event(...)
    _inherit = "event.calendar"
    category_id = Fields.many2one(....)

    def _auto_init(self, cr, context=None):
        cr.execute("update event_calendar set category_id = 1 where category_id is null")

I hope it will resolve your issue.

Thanks.

Thank you! This was the method i was searching for!

Michael Karrer
on 8/11/15, 9:23 AM
1
Marvin Taboada
On 8/12/15, 12:04 AM

Michael, `_auto_init()` is an ORM method not expected to be completely redefined because it does important stuff during module initialization, please see:

https://github.com/odoo/odoo/blob/8.0/openerp/models.py#L2416 

You can safely override this method provided you also invoke the parent's class implementation as it is done in several models from the base module, e.g.:

https://github.com/odoo/odoo/blob/8.0/openerp/addons/base/ir/ir_attachment.py#L217

    def _auto_init(self, cr, context=None):
        super(ir_attachment, self)._auto_init(cr, context)
        # Now safely perform your own stuff
        cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_attachment_res_idx',))
...

The method I prefer for these cases is to add an `init(self, cr)` method on models. This method is invoked after `_auto_init()` provided it is defined in the model, see the following link for details about how both methods are invoked:

https://github.com/odoo/odoo/blob/8.0/openerp/modules/module.py#L274 

Have a nice day.

Thank you very much!

Michael Karrer
on 8/12/15, 2:17 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)

Stats

Asked: 8/11/15, 8:44 AM
Seen: 1571 times
Last updated: 8/12/15, 10:38 AM