콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
3 답글
465 화면

I wanted to show the number of days (0 day, -2 days, 3 days etc) for the Integer field "Days Left". 

Assuming today date is 09-06-2025(DD-MM-YYYY), this is how the result is showing up in the view. The problem here is that the result is empty(not showing up) when the dates difference is equal to 0 Day. 

Product Name-----Expiry Date-----Days Left

Product 1 ---------07-06-2025------    -2

Product 2 --------09-06-2025 ------  (empty)

Product 3 ---------12-06-2025 -------   3


Compute function for "Days Left"

class Drug(models.Model):
_inherit = "product.template"

expiry_date = fields.Date(string="Expiry Date")
due = fields.Integer(string="Days Left", compute="_compute_due")

def _compute_due(self):
for record in self:
if record.expiry_date:
fmt = '%Y-%m-%d'
from_date = record.expiry_date.strftime(fmt)
today_date = date.today()
if today_date == record.expiry_date:
record.due = 0
else:
now = today_date.strftime(fmt)
d1 = datetime.strptime(from_date, fmt)
d2 = datetime.strptime(now, fmt)
print(from_date, today_date, now, d1, d2)
record.due = str((d1 - d2).days)
else:
record.due = ""

Thank you for your suggestion!

아바타
취소
작성자 베스트 답변

Hi, I've changed the code as per your suggestion. But It is still not showing in the list view when Days Left is equal to 0. (View Name : product.template.product.list) (it works fine in the form view, Thanks)

Product Name.    Expiry Date.                   Days Left

Product 1.            14 Jun 2025(Today)           1

Product 2.           13 Jun 2025                      (empty)


아바타
취소
베스트 답변

Hi,


Change the function as follows,


def _compute_due(self):
for record in self:
if record.expiry_date:
today_date = date.today()
# No need to convert to string and back
delta_days = (record.expiry_date - today_date).days
record.due = delta_days
else:
record.due = 0


Result-



Hope it helps

아바타
취소
관련 게시물 답글 화면 활동
1
7월 23
3170
4
12월 22
9130
2
4월 21
3272
1
10월 18
4626
1
10월 16
4081