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.

0

Field function definition

By
Koffi KPONOUGLO
on 10/9/13, 4:13 PM 2,872 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.

1
nazarii
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.

Koffi KPONOUGLO
on 10/9/13, 5:56 PM
0

Serpent Consulting Services Pvt. Ltd.

--Serpent Consulting Services Pvt. Ltd.--
4341
| 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.

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

0 follower(s)

Stats

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