Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
1 ตอบกลับ
4734 มุมมอง

Looking to show the age of an item created with a form. I have the fields:

checkin_date = fields.Datetime(default=fields.Datetime.now,readonly=True)
today = fields.Datetime(default=fields.Datetime.now)

age=fields.Integer(string="Age")

'checkin_date' is set as the current date and time whenever the form is submitted, and 'today' should be the current date and time. I want to know the age of the tickets (ie. 'today' - 'checkin_date' = 'age' #x number of days).

EDIT: the way I have 'today' set up is apparently not correct. If I put a field on the form using 'today' it appears blank. It is not actually pulling the current date. Think this may be my problem.

I have this set up currently:

    @api.onchange('checkin_date', 'today', 'age')
    def calculate_date(self):
        if self.checkin_date and self.today:
            d1=datetime.strptime(str(self.checkin_date),'%Y-%m-%d')
            d2=datetime.strptime(str(self.today),'%Y-%m-%d')
            d3=d2-d1
            self.age=str(d3.days)

But it results in just a 0 in the field of 'age'. If I change the 'checkin_date', nothing changes and 'age' remains at zero. I am hoping for the 'age' to automatically adjust each day as the date changes so we can easily see how old a item is. 

Note: The checkin_date works properly with the date when items are made and datetime is imported. 


Odoo V11

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

    @api.onchange('checkin_date', 'today', 'age')
@api.one def calculate_date(self):

I don't test it.



อวตาร
ละทิ้ง
ผู้เขียน

added @api.one and nothing changed. Age is still showing 0.

Related Posts ตอบกลับ มุมมอง กิจกรรม
create field calculated with dates แก้ไขแล้ว
2
พ.ค. 21
4053
2
ม.ค. 24
2674
2
มิ.ย. 22
4609
2
มิ.ย. 18
6143
2
พ.ย. 16
13771