Odoo Help


Field function definition

on 10/9/13, 4:13 PM 4,038 views

I'm a beginner in python.

Please i am trying to define in my new module for manage Human resource posting, a field fonction named "date_to" which have to display the end date of the employee posting. I use two step to do this: fonction and field definition

Here is the fonction code

#Automatic determination of field date_to

def compute_date_to(self, date_from, date_to): # I define date_from and date_to field.datetime in the object hr.posting
    """Returns a end date for hr.posting ."""

    DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
    from_dt = datetime.datetime.strptime(date_from, DATETIME_FORMAT)
    to_dat = datetime.datetime.strptime(date_to, DATETIME_FORMAT)      
    to_dat = from_dat + timedelta(days=1826)   # Here I'm fixing the timedelta days(1826 days = 5years for me) between date_from and date_to
    return to_dat

Here is the field code

'date_to': fields.function(compute_date_to, string='Date Departure', type='datetime', readonly=True, store=True), The module is installed correctly but when i try to save one posting i have this error

Client Traceback (most recent call last): File "/home/benos/Public/latest/openerp-7.0-20130723-231040/openerp/addons/web/http.py", line 204, in dispatch response["result"] = method(self, *self.params) File "/home/benos/Public/latest/openerp-7.0-20130723-231040/openerp/addons/web/controllers/main.py", line 1130, in call_kw return self._call_kw(req, model, method, args, kwargs) File "/home/benos/Public/latest/openerp-7.0-20130723-231040/openerp/addons/web/controllers/main.py", line 1122, in _call_kw return getattr(req.session.model(model), method)(args, **kwargs) File "/home/benos/Public/latest/openerp-7.0-20130723-231040/openerp/addons/web/session.py", line 42, in proxy result = self.proxy.execute_kw(self.session._db, self.session._uid, self.session._password, self.model, method, args, kw) File "/home/benos/Public/latest/openerp-7.0-20130723-231040/openerp/addons/web/session.py", line 30, in proxy_method result = self.session.send(self.service_name, method, *args) File "/home/benos/Public/latest/openerp-7.0-20130723-231040/openerp/addons/web/session.py", line 103, in send raise xmlrpclib.Fault(openerp.tools.ustr(e), formatted_info)

Server Traceback (most recent call last): File "/home/benos/Public/latest/openerp-7.0-20130723-231040/openerp/addons/web/session.py", line 89, in send return openerp.netsvc.dispatch_rpc(service_name, method, args) File "/home/benos/Public/latest/openerp-7.0-20130723-231040/openerp/netsvc.py", line 292, in dispatch_rpc result = ExportService.getService(service_name).dispatch(method, params) File "/home/benos/Public/latest/openerp-7.0-20130723-231040/openerp/service/web_services.py", line 626, in dispatch res = fn(db, uid, params) File "/home/benos/Public/latest/openerp-7.0-20130723-231040/openerp/osv/osv.py", line 188, in execute_kw return self.execute(db, uid, obj, method, *args, *kw or {}) File "/home/benos/Public/latest/openerp-7.0-20130723-231040/openerp/osv/osv.py", line 131, in wrapper return f(self, dbname, args, *kwargs) File "/home/benos/Public/latest/openerp-7.0-20130723-231040/openerp/osv/osv.py", line 197, in execute res = self.execute_cr(cr, uid, obj, method, args, *kw) File "/home/benos/Public/latest/openerp-7.0-20130723-231040/openerp/osv/osv.py", line 185, in execute_cr return getattr(object, method)(cr, uid, args, *kw) File "/home/benos/Public/latest/openerp-7.0-20130723-231040/openerp/addons/mail/mail_thread.py", line 242, in create thread_id = super(mail_thread, self).create(cr, uid, values, context=context) File "/home/benos/Public/latest/openerp-7.0-20130723-231040/openerp/osv/orm.py", line 4478, in create self.pool.get(object)._store_set_values(cr, user, ids, fields2, context) File "/home/benos/Public/latest/openerp-7.0-20130723-231040/openerp/osv/orm.py", line 4627, in _store_set_values result = self._columns[f].get(cr, self, ids, f, SUPERUSER_ID, context=context) File "/home/benos/Public/latest/openerp-7.0-20130723-231040/openerp/osv/fields.py", line 1133, in get result = self._fnct(obj, cr, uid, ids, name, self._arg, context) TypeError: compute_date_to() takes exactly 3 arguments (7 given)

i know i choose the wrong way help me.




| 5 4 7
On 10/9/13, 4:41 PM

First of all this method has wrong arguments for functional field computation, it need to looks like def compute_date_to(self, cr, uid, ids, field_name, args, context=None): More you can read here. Second error is you are returning wrong value and you will get error when you fix previous one(you must return dict with value for each id from ids). Also value you computed needs to be converted to string representation of datetime field. If you use exact code provided you will also get an error Name 'from_dat' is not defined. Good luck!

Thanks. i will try harder.

on 10/9/13, 5:56 PM

Serpent Consulting Services Pvt. Ltd.

--Serpent Consulting Services Pvt. Ltd.--
| 6 6 8
Gandhinagar, India
--Serpent Consulting Services Pvt. Ltd.--

Serpent Consulting Services Pvt. Ltd. Your Odoo/OpenERP Solution, just an email away!

Serpent Consulting Services Pvt. Ltd.
On 12/16/13, 3:04 AM

Hello Friend,

Actually there are two problems.

(1) There is problem in method definition for function field.

It should be like:

def compute_date_to(self, cr, uid, ids, field_name, args, context=None):

(2) Another problem is with return value:

It should be dictionary with key = Record ID and value = Value.

Ex. { 1 : '2013-11-11 13:00:00' }

You can apply your own logic inside method.

Thank you.

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

Question tools

0 follower(s)


Asked: 10/9/13, 4:13 PM
Seen: 4038 times
Last updated: 3/16/15, 8:10 AM