2
votes

In odoo 10, I'd like to have a computed field that calculate every day difference between current date and other date: This is my code:

Python :

days_number = fields.Integer('Days remaining', compute='_compute_remaining_days', store=True)
@api.depends('maturity_date')
def _compute_remaining_days(self):
    current_date = datetime.datetime.now().date()
    date1 = False
    if self.maturity_date:
        date1 = datetime.datetime.strptime(self.maturity_date, "%Y-%m-%d")

    date2 = datetime.datetime.strptime(str(current_date), "%Y-%m-%d")
    date3 = date1 - date2
    date = int(date3.days)
    self.days_number = date

XML:

 <record id="ir_cron_remain_days" model="ir.cron">
    <field name="name">Remaining Days</field>
    <field eval="True" name="active"/>
    <field name="user_id" ref="base.user_root"/>
    <field name="interval_number">1</field>
    <field name="interval_type">days</field>
    <field name="numbercall">-1</field>
    <field eval="False" name="doall"/>
    <field name="model">tresorerie_test.account.payment.maturity</field>
    <field name="function">_compute_remaining_days</field>
    <field name="args">()</field>
</record>

The problem that the field doesn't get the value automatically.

Can you help me to solve this ?

Kind regards

1
You need to use timedelta to express the difference between two dates - ChesuCR
I would remove the compute method from the field, the automatic function should update the field. The compute method on a field is executed each time the value is needed - ChesuCR

1 Answers

1
votes
  • If you have given your 'days_number' field in xml view then remove store=True it will work as desired.
  • There no need to type cast in integer you already getting an integer

    start = date(2018,04,03) end = date(2018,04,10) ans = end - start print ans.days