Odoo Help


2 Answers
Shaun Dawson United States
8/10/13, 1:16 AM

Yeah, on_change does not seem to be supported for calendar fields, which makes sense I suppose.

The way that I accomplished this in my module was to override the Model class 'write' method:

def write(self, cr, uid, ids, vals, context=None):
    print "in write, vals: ", vals
    #fiddle with vals to change what gets written to the DB
    return super(class_name, self).write(cr, uid, ids, vals, context=context)

Jeudy Nicolas France

--Jeudy Nicolas--

| 6 8 10
Pelousey, France
--Jeudy Nicolas--

- Linux administrator - Functional guy with OpenERP - Python, html, css, javascript developer OpenERP Ready Partner

Jeudy Nicolas France
8/21/13, 3:57 PM


I use a small hack on each model definition to track changes when writing them.

first, define which field you want to watch:

  • first parameter: field name you want to watch
  • second one: function you want to launch if it change

Here is an example for project.project model:

_watch_attrs = {'stage_id': 'on_change_stage_id', 'project_id': 'on_change_project_id'}

then add this new write function to handle this

def write(self, cr, uid, ids, vals, context=None):

    ## On Change attributes checks
    objs = self.browse(cr, uid, ids)
    for attr, on_change_label in self._watch_attrs.iteritems():
        if attr in vals:
            if any(obj[attr] != vals[attr] for obj in objs):
                vals = getattr(self, on_change_label)(cr, uid, ids, vals, context=context)

    return super(my_inheritedclass, self).write(cr, uid, ids, vals, context)

you now just have to défine what you want in the function that is executed when watching changes:

def on_change_project_id(self, cr, uid, ids, vals, context):
    task_obj = self.pool.get('project.task')
    tasks = self.browse(cr,uid,ids,context=context)[0].tasks_id
    if vals.get('project_id',False):
        new_project = vals.get('project_id',False)
        for task in tasks:
            #DO SOMETHING like chatter post message or action ..

    return vals

So if ou drag an drop event on calendar, you car watch date_start or date_end and execute one specific method. It is very usefull to track stage_id changes when clicking on status widget.

Ask a Question
Keep Informed
2 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.

Odoo Training Center

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

Test it now