Odoo Help

Welcome!

This community 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.

5

How to put an onchange event on a calendar view?

By
Bonnet Denis
on 2/14/13, 12:04 PM 3,275 views

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">project.task.work.form</field>
        <field name="model">project.task.work</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}"/>
            </calendar>
        </field>
    </record>

Cordialement.

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

Bonnet Denis
on 2/19/13, 7:02 AM

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"

Serpent Consulting Services Pvt. Ltd.
on 5/18/13, 2:03 AM

Thanks, I will try this week.

Bonnet Denis
on 5/21/13, 5:34 AM
1
Shaun Dawson
On 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)
0

Jeudy Nicolas

--Jeudy Nicolas--
1181
| 6 7 8
PELOUSEY, France
--Jeudy Nicolas--

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

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

Hello,

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.

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 2/14/13, 12:04 PM
Seen: 3275 times
Last updated: 3/16/15, 8:10 AM