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

I'm trying to formulate the following fields:

Fields are as follows:

Date_one

Date_two

Int_one (input the number of days)

date_total


Here's the computation:

date_total = (Date_two - Date_one) + 1 - Int_one


How should I do this here? and what are the dependents?

for record in self:
  record['size'] = len(record.name)


Thank you in advance!

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

Hi Neil,

Try,

today = datetime.date.today()
xmasday = datetime.date(2022, 12, 25)
no_of_days = 5
total_days = (xmasday - today).days + 1 - no_of_days

Hope it helps,
Kiran K

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

Hi Kiran,

I am getting this error:
ValueError: <class 'TypeError'>: "unsupported operand type(s) for -: 'datetime.date' and 'bool'" while evaluating

its because one of the date fields does not contain any values, So it returns False ,

if today and xmasday:
total_days = (xmasday - today).days + 1 - no_of_days
else:
raise UserError(_('Update Date Field'))

คำตอบที่ดีที่สุด

Assuming you are using computed fields:

record ['date_total'] = record.date_two - datetime.timedelta(days=record.date_one + record.int_one - 1)

Add an 'if' statement if there is an error when creating new records. (if the dates are custom their default value 'None' which can't be computed)

The dependencies are fields which trigger the recomputation when changed, so that will be specific to your scenario.

- I am not a programmer so the above code can likely be improved. Hopefully someone else can help here?


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

Hi Lucas,

I am getting this error: "function missing required argument 'year'

Please give the exact conditions to recreate, are you using Studio? Odoo version? etc

Related Posts ตอบกลับ มุมมอง กิจกรรม
Add Any Number To Date แก้ไขแล้ว
2
ธ.ค. 23
7257
Only month and year in a date แก้ไขแล้ว
4
ธ.ค. 23
24229
Simple Computed Field help แก้ไขแล้ว
3
ก.ย. 24
1421
0
ธ.ค. 23
1355
0
ต.ค. 21
2947