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.

1

Set default duration in res_config

By
Lucio
on 2/7/13, 10:00 AM 3,118 views

I have a module that has, a creation date (of course, the day it was created) and a deadline date.

The thing is that I want to be able to set a default value to this deadline date according to the date it was created. But I want to be able to configurate this dynamically from (lets say) res_config.

e.g.

If I configured the default values in res_config to 5 days, I want the default value for the deadline date to be populated with a date 5 days ahead of the creation date.

Is this possible?

Thank you

1
Lucio
On 2/8/13, 11:14 AM

I managed to do this using the model ir.configure_parameter.

In res_config.py:

class my_configuration(osv.osv_memory):
    _inherit = ['res.confi.settings']
    _columns = {
       'default_deadline' : fields.integer('Days per default', help="""Help field"""),
    }
    ...
    def set_default_deadline(self, cr, uid, ids, context=None):
        config = self.browse(cr, uid, ids)
        config = config and config[0]
        val = '%s' %(config.default_deadline) or '10'
        self.pool.geet('ir.config_parameter').set_param(cr,uid, 'key_value', val)
        return True

Whit this we have created a system parameter. It is actually created as a mapping from 'key_value' to val that is a string, so we will have to cast it to the desired type when necessary. In my case, y created a function to get the deadline date in my module:

def _get_deadline_date(self, cr, uid, context=None):
    val = self.pool.get('ir.config_parameter').get_param(cr, uid, 'key_value')
    try:
        val = int(val)
    except: 
        # Just in case...
        val = 30
    return (datetime.now() + timedelta(days=val)).strftime('%Y,%m,%d')

_defaults = {
    'deadline_date': lambda s, cr, uid, c: s._get_deadline_date(self, cr, uid,     context=c),
}

Thank you, hope it helps!

you will need to give only default_model='' " in field ..then no req to over write get and set method ..it should work automatically..whre ever u want to set deadline there you have to get this value and set deadline

Turkesh Patel (tpa)
on 2/11/13, 7:00 AM

Sorry, but I don't know what you mean with the field 'default_model'. Where is it? Actually, my idea wasn't to over write the get/set method, it is just a function to get the default value for the field (maybe the name picking was not the most accurate!). Thanks!

Lucio
on 2/13/13, 9:09 AM
0
Mike Telahun
On 3/20/13, 10:25 AM

To expand on Turky's comment: You don't need to write your own functions to set and get the default value. It is enough that you add the 'default_model' parameter to your column definition:

class my_configuration(osv.osv_memory):
    _inherit = ['res.config.settings']
    _columns = {
       'default_deadline' : fields.integer('Days', default_model='object.name'),
    }

For this to work correctly you will need:

  • a field named 'deadline' in your 'object.name' object
  • the column providing the default value in your res.config.settings class must begin with default_

Good one! I did not know that! Nevertheless, the thing is that I am actually setting a numeric value, representing the numbers of days for default for a the objects of my model, so, the default value for deadline_date must be (x days + cration_date) where x is the integer that you can set in my_configuration.

Lucio
on 3/20/13, 11:59 AM

As long as the field type for the default value in res.config and the field type in your object are the same. OpenERP will do the right thing.

Mike Telahun
on 3/20/13, 12:53 PM

Yes, that's the thing in my case, they where not! Because I wanted to set a date "on the fly", adding a time delta to the creation date. Setting a date as default will just set in all records the field with the value in res.config. Hope I am not missing something here!

Lucio
on 3/21/13, 8:59 AM

I'm not sure if I understand correctly, but here goes: In your main object you should have three fields: begin_date, deadline_date and deadline_delta. The begine_date and deadline_date fields are date fields and deadline_delta would be an integer field. Then in res.config you would have a field called default_deadline_delta, which is an integer. Then in your main object you would have an onchange method on the begin_date field, which when triggered would change the value of deadline_date by adding deadline_delta to begin_date. I hope I have correctly understood what you want to do.

Mike Telahun
on 3/21/13, 10:48 AM

Yes, the answer you are giving me fit my requirements perfectly! The thing is that, by doing so, I would create a field in the database for every record of my model, that is to be filled on creation time and never to be modified again. (If you want to change the deadline_date you should change it directly not changing the deadline_delta). Thanks for your concern! :D

Lucio
on 3/21/13, 11:03 AM
0
Turkesh Patel (tpa)
On 2/8/13, 9:31 AM

yes this is possible using resconfig.you can define defaultdeadline field.

I did not find the way to do that using this, because is not like I want a fix date, I want the deadline date to be, for example, 4 days after the creation date.

Can you explain to me if you know how to do this?

Lucio
on 2/8/13, 11:13 AM

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

1 follower(s)

Stats

Asked: 2/7/13, 10:00 AM
Seen: 3118 times
Last updated: 3/16/15, 8:10 AM