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

Add month to date field

By
Victor Godayer
on 3/23/15, 6:51 AM 2,286 views

Hi,


I have a model with a date field as follow :


class myModel(osv.osv)

    date_start : fields.date(...)

    month_nb = fields.integer(...)

    date_end = fields.date(...)


I made an onchange method to calculate the date_end field on when month_nb and date_start change :

@api.onchange('date_start','month_nb')

def _compute_date_end(self):

      from datetime import date

      from dateutil.relativedelta import relativedelta

      self.date_end = self.date_start + relativedelta(months=self.month_nb)


But with this code, I get an error :

self.date_end = self.date_start + relativedelta(months=self.month_nb)

File "/usr/lib/python2.7/dist-packages/dateutil/relativedelta.py", line 247, in __radd__

raise TypeError, "unsupported type for add operation"

TypeError: unsupported type for add operation


I don't understant what I am doing wrong in my code. Do you see something wrong in my code ?

Thank you for your help

0
Victor Godayer
On 3/25/15, 5:09 AM

I eventually found the solution with the old api way :


    from datetime import datetime

    from dateutil.relativedelta import relativedelta

    if self.date_start:

        self.date_end = (datetime.strptime(self.date_start,'%Y-%m-%d') + relativedelta(months=self.duree_mois)).strftime('%Y-%m-%d')

2

zbik

--zbik--
5941
| 6 4 7
Lodz, Poland
--zbik--


zbik
On 3/24/15, 10:54 AM

1. if you use the new api @api.onchange, you create model definition in new api standard, see documentation

class myModel(models.Model)

date_start : fields.Date(...)

month_nb = fields.Integer(...)

date_end = fields.Date(...)


2. You use helpers for date conversion

date_start_dt = fields.Datetime.from_string(self.date_start)

dt = date_start_dt + relativedelta(months=self.month_nb)

self.date_end = fields.Datetime.to_string(dt)

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: 3/23/15, 6:51 AM
Seen: 2286 times
Last updated: 3/25/15, 5:09 AM