2

I would like to add three years to the existing fields.date (date_entree) and get back the result in the field date_fin_prev in Odoo.

The first approach is:

from odoo import fields,api,models
from datetime import datetime

class Ca_administrateur (models.Model):
_name= "ca_administrateur"
date_entree= fields.Date(string="Date d'entrée")
date_fin_prev= fields.Date(compute="_compute_date_fin_prev", store=False)
@api.multi
@api.depends('date_entree')
def _compute_date_fin_prev(self):
    for record in self:
    record.date_fin_prev = record.date_entree + datetime.timedelta(years=3)

The error generates is "AttributeError: 'ca_administrateur' object has no attribute '_compute_date_fin_prev'"

When i try the second approach

date_fin_prev= fields.Date()
@api.multi
@api.onchange('date_entree')
def on_change_state(self):
    for record in self:
    record.date_fin_prev = record.date_entree + datetime.timedelta(years=3)

But when i clicks in "save" on the interface, it's not effect in the table date_fin_prev

  • Do you want that the user can later modify `date_fin_prev` , or that value must always be computed automatically? Moreover, you said "three years", it's a fixed delay, or you are considering the value from `duree` ? – luca.vercelli Jun 06 '19 at 11:58
  • @luca.vercelli yes i think that's what he is trying to do. – CZoellner Jun 06 '19 at 12:14
  • just to be sure. In the first approach, did you indented correctly the code? indentation seems wrong in your post – luca.vercelli Jun 09 '19 at 21:18
  • Yes, it was a indented problem but the new message error is: AttributeError: type object 'datetime.datetime' has no attribute 'timedelta' – Hyacinthe OUATTARA Jun 11 '19 at 20:03
  • try from datetime import timedelta if you still have that – Derick Jun 17 '19 at 05:19

1 Answers1

1

Edit: I go to show two different approaches, depending on what you want to achieve.

First approach: a computed field. date_fin_prev is computed and cannot be modified by the user

date_fin_prev= fields.Date(compute="_compute_date_fin_prev", store=False)
@api.multi
@api.depends('date_entree')
def _compute_date_fin_prev(self):
    for record in self:
        record.date_fin_prev = record.date_entree + datetime.timedelta(years=3)  # Actually not checked

Second approach: an "onchange" method, that is called by the interface whenever the user changes the value of the first field

date_fin_prev= fields.Date() # not computed
@api.multi
@api.onchange('date_entree')
def on_change_state(self):
    for record in self:
        record.date_fin_prev = record.date_entree + datetime.timedelta(years=3)  # Actually not checked
luca.vercelli
  • 898
  • 7
  • 24
  • When I take the first approach, here is the error message that it generates AttributeError: 'ca_administrateur' object has no attribute '_compute_date_fin_prev'. "ca_administrateur" is the name of my class. the second approach has no effect in the table date_fin_prev – Hyacinthe OUATTARA Jun 07 '19 at 11:34
  • In the first approach, did you check the name of the method? it must be "_compute_date_fin_prev", i.e. the same name declared in the clause "compute=..." . In the second approach, the table should have a column "date_fin_prev" and this column should be updated whenever the user clicks "save" on the interface. – luca.vercelli Jun 07 '19 at 12:27
  • Yes, it is the same information! I just updated my question. you can refer to it and see my code. – Hyacinthe OUATTARA Jun 07 '19 at 16:46