This question has been flagged
3 Replies
9240 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

Avatar
Discard
Author Best Answer

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!

Avatar
Discard

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

Author

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!

Best Answer

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

Avatar
Discard
Author

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?

Best Answer

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_
Avatar
Discard
Author

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.

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.

Author

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!

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.

Author

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