Odoo Help

3

1
5 Answers
4
Avatar

Emipro Technologies Pvt. Ltd. India

--Emipro Technologies Pvt. Ltd.--
7823
| 7 8 10
Rajkot, India
--Emipro Technologies Pvt. Ltd.--

Emipro has been providing a wide range of business solutions in the domain of ERP & eCommerce. We take pride in serving 1000s of contented customers across the globe with strong roots in the US, Europe & India. Being a Certified Odoo Gold Partner in India as well as the United States, we bring our rich professional experience and unrivaled technical competence to help our customers obtain efficient and cost-effective business solutions in Odoo. With our team of 50+ Odoo Experts and highly experienced business analysts, we serve our customers which are companies of all sizes, ranging from start-ups to large enterprises. 

Our ERP Services:

  • Business Need Analysis & GAP Analysis

  • ERP Migration

  • Odoo Consultancy

  • Odoo Implementation & Customization

  • Odoo Training & Support

  • Odoo Website & eCommerce Development

  • Odoo Integration (Marketplaces, eCommerce platforms, Shipping Carriers, Payment Gateways etc.)

Our Achievements:


Reach Us:

Website: www.emiprotechnologies.com

Email: info@emiprotechnologies.com

Skype: emiprotech

Emipro Technologies Pvt. Ltd. India
8/11/15, 9:16 AM

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.

1 Comment
Michael Karrer Austria
8/11/15, 9:23 AM

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

1
Avatar

Marvin Taboada Bolivia

--Marvin Taboada--

761
| 4 3 8
Cochabamba, Bolivia
--Marvin Taboada--
Marvin Taboada Bolivia
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.

1 Comment
Michael Karrer Austria
8/12/15, 2:17 AM

Thank you very much!

0
Jérôme Thériault Canada
1/30/19, 4:10 PM

Old question but still interresting for newer versions. In 12.0 for example,  _auto_init() and init() are fine for indexes but for NOT NULL constrains, by the time those methods are executed, it's too late and the SQL constraints have already been applied and caused a NOT NULL constraint error due to existing rows with null data. It seems the way to handle this properly is by overriding the _init_column() method, check for the column name and do the SQL UPDATE statements in there:

    @api.model_cr_context
    def _init_column(self, column_name):
        # Set a value on existing data for required fields
        if column_name == 'real_check_in':
            self.env.cr.execute("UPDATE hr_attendance SET real_check_in = check_in WHERE real_check_in IS NULL")
        elif column_name == 'real_check_out':
            self.env.cr.execute("UPDATE hr_attendance SET real_check_out = check_out WHERE real_check_out IS NULL")
        super()._init_column(column_name)


Ask a Question
Writer
Keep Informed
1 follower(s)
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