Odoo Help


Sample code of computation

on 2/8/15, 7:56 PM 531 views

alright, because what i want is to subtract the dateofinspection in yeareffect. so any suggestion what im going to use or much better code to do that.???

class appraisal-report (osv.Model):

                _name = 'appraisal.report'

               _description = 'Appraisal Report'

              _columns = {

                        'dateofinspection': fields.date ('Date of Inspection'),

                       'yeareffect': fields.integer ('Year Effect' size=64, required=True)



from openerp.osv import orm, fields
from dateutil.relativedelta import relativedelta
from datetime import datetime
from openerp import tools

class appraisal-report (orm.Model):
     _name = 'appraisal.report'
     _description = 'Appraisal Report'

    def difference_year(self, cr, uid, ids, name, args, context=None):
        res = {}
        for record in  self.browse(cr, uid, ids, context=context):
            yeareffect = datetime.strptime(record.yeareffect, '%Y') # Your year field
            dateofinspection = datetime.strptime(record.dateofinspection, tools.DEFAULT_SERVER_DATE_FORMAT) # Your date field
            difference = relativedelta(yeareffect, dateofinspection).years # (01-01-2020, 10-02-2015)
            res[record.id] = str(difference)
        return res
    _columns = {
        'dateofinspection': fields.date ('Date of Inspection'),
        'yeareffect': fields.char('Year Effect', size=64, required=True),
        'difference': fields.function(difference_year, string="Difference", type='char')

On 2/10/15, 4:41 AM

hi, i try your code and it works on me.. tnx

Always try to accept the answer by clicking the tick mark if it works for you, so that in future people can use it.

Dhinesh - Technical Consultant, Sodexis Inc
on 2/10/15, 5:01 AM

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

1 follower(s)


Asked: 2/8/15, 7:56 PM
Seen: 531 times
Last updated: 3/28/15, 3:13 AM