Skip to Content
Menu
This question has been flagged
1 Reply
2338 Views

I have a date field in form that allows the users to enter a date which presumably the deadline of a task and I want to have difference between today with deadline and concern of: 1.if the date has passed, then show 'overdue' 2.if not, then show the number of days left the given date and add text that say 'the left day are {%results of difference days%}' which field type can aggregate integer with text

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


class todotask(models.Model):
    _name='todo.task'
    _description='to manage your job tasks'

    name=fields.Char('Description', required=True)
    gov_deprt_id=fields.Many2many('res.partner',string='Gov 
                 Department')
    company=fields.Many2one('res.partner',
            string='Work For')
    start_date=fields.Date('Start Date')
    deadline_date=fields.Date('Deadline')
    is_done=fields.Boolean('Done?')
    note=fields.Text('Note')
    amount=fields.Float('Cost Amount')
    remaining_days=fields.Integer(string="Remaining Days")

@api.onchange('start_date', 'deadline_date', 'remaining_days')

def calculate_date(self):
     while self.start_date and self.deadline_date:
        d1 = datetime.strptime(str(self.start_date), '%Y-%m-%d')
        d2 = datetime.strptime(str(self.deadline_date), '%Y-%m-%d')
        d3 = d2 - d1
        self.remaining_days ="{} and {}".format("tttt", str(d3.days))
Avatar
Discard
Best Answer

You can do the string logic in the view, instead of the model.

I would make the remaining_days field a computed field and make the calculate_date method set this field.
To respect the Odoo developer guidelines, name the method compute_remaining_days ;)

In the view, you can check remaining_days, and then make the view show/hide the fields and text you want.

BTW: You got some indentation errors in your code. The decorator and function must have the same indentation level as the field declarations.
Also, do you want to pick multiple Gov Departments from res.partner? If so, name the field gov_deprt_ids
If you want to pick just one, use a Many2one field, and name the field as you have done with "_id".
Do the same for any relational field in your model (hint: company)

Try this out for your model:

from odoo import api, fields, models


class TodoTask(models.Model):
    _name = 'todo.task'
    _description = 'Todo Task'
    
    name = fields.Char('Description', required=True)
    gov_deprt_id = fields.Many2one('res.partner', string='Gov Department')
    company_id = fields.Many2one('res.partner', string='Work For')
    start_date = fields.Date('Start Date')
    deadline_date = fields.Date('Deadline')
    is_done = fields.Boolean('Done?')
    note = fields.Text('Note')
    amount = fields.Float('Cost Amount')
    remaining_days = fields.Integer(
        string="Remaining Days",
        compute=_'compute_remaining_days'
    )

    @api.depends('deadline_date')
    def _compute_remaining_days(self):
        for task in self:
            if task.deadline_date:
                time_delta = task.deadline_date - fields.Date.today()
                task.remaining_days = time_delta.days

As for the view, the Odoo Docs are well documented. There are many ways you could do this.. so giving a specific example is hard. 

If you want to calculate the message to the user in python, you can make a Char/text field called for example "Message" and then compute this message with a method (like with remaining_days). Make it readonly so people can't edit it.
Making a string like you mentioned doesn't necessarily fit very well with Odoo's way of doing form views etc. Showing "Overdue" or days remaining on a smart button, or use states in the statusbar would be more Odoo-like, in my opinion.

I would also recommend reading the developer guidelines as well as PEP8 (get a linter for you editor, it helps learning - a lot!).

Hope it helps! :D


Avatar
Discard
Related Posts Replies Views Activity
3
Oct 23
5981
1
Sep 23
1971
1
May 23
1005
2
Apr 23
1378
1
Mar 23
996