How to put an onchange event on a calendar view?

Bonnet Denis

In my personal add-on I've made some calendar,form and tree view. the onchange events work for form and tree view but I don't find how to handle a change of date or duration.

My calendar view:

    <record model="ir.ui.view" id="task_work_calendar">
        <field name="name"></field>
        <field name="model"></field>
        <field name="type">form</field>
        <field name="priority" eval="1"/>
        <field name="arch" type="xml">
            <calendar string="Planning des Taches" date_start="date" date_delay="hours" color="user_id" mode="week">
                <field name="id" invisible='1'/>
                <field name="name" on_change="onchange_name(name,id)" />
                <field name="date" on_change="onchange_date(date,id)" invisible='1'/>
                <field name="hours" on_change="onchange_hours(hours,id)" invisible='1'/>
                <field name="user_id" on_change="onchange_user_id(user_id,id)" modifiers="{'required': true}"/>


Bonnet Denis

I've checked how work a calendar view. Bad news: it's only javascript and I can't use onchange event on calendar's fields(I can write it but it's not use => no Error in the log file). Someone can help me or have a trick to make this??? Thanks

Please remove this field <field name="type">form</field> and try it because in openerp v7 the type field is not medatory and in calendar tag define version="7.0"

Bonnet Denis

Thanks, I will try this week.

2 Answers
Shaun Dawson
Best Answer

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
Best Answer


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.