I have a computed field that calculates the time duration between today, and when a person joined the company. It works, as long as the dependent field (x_studio_join_date) already contains something. Buf if it is empty, like when I create a new record, it fails with the following error.
Clearly the computed field is being called before the record has been initialized. The 'required' attribute has had no effect. I thought the conditional test (if record['x_studio_join_date'] != False:) would prevent the computation from accessing a still null field, but this doesn't seem to work.
Any help would be appreciated.
Dependencies: x_studio_join_date
Compute:
for record in self:
if record['x_studio_join_date'] != False:
start_date = record.x_studio_join_date
end_date = datetime.date.today()
time_delta = end_date - start_date
years, remainder_days = divmod(time_delta.days, 365.25)
months, days = divmod(remainder_days, 30.44)
if years > 0:
txt = '{:}y {:}m {:}d'.format(int(years), int(months), int(days))
elif months > 0:
txt = '{:}m {:}d'.format(int(months), int(days))
elif days > 0:
txt = '{:}d'.format(int(days))
else:
txt = "n/a"
record['x_studio_duration'] = txt
Returns the following error:
RPC_ERROR
Odoo Server Error
Traceback (most recent call last):
File "......api.py", line 958, in get
cache_value = field_cache[record._ids[0]]
KeyError:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "....fields.py", line 1158, in __get__
value = env.cache.get(record, self)
File "....api.py", line 965, in get
raise CacheMiss(record, field)
odoo.exceptions.CacheMiss: 'hr.employee(,).x_studio_duration'
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Accounting
- Inventory
- PoS
- Project
- MRP
This question has been flagged
1
Reply
4031
Views
Hi,
You can add a else condition when record['x_studio_join_date'] = False and assign the value as record['x_studio_duration'] = ''
for record in self:
record['x_studio_duration'] = False
if record['x_studio_join_date'] != False:
start_date = record.x_studio_join_date
end_date = datetime.date.today()
time_delta = end_date - start_date
years, remainder_days = divmod(time_delta.days, 365.25)
months, days = divmod(remainder_days, 30.44)
if years > 0:
txt = '{:}y {:}m {:}d'.format(int(years), int(months), int(days))
elif months > 0:
txt = '{:}m {:}d'.format(int(months), int(days))
elif days > 0:
txt = '{:}d'.format(int(days))
else:
txt = "n/a"
record['x_studio_duration'] = txt
Regards
Enjoying the discussion? Don't just read, join in!
Create an account today to enjoy exclusive features and engage with our awesome community!
Sign upRelated Posts | Replies | Views | Activity | |
---|---|---|---|---|
|
1
May 25
|
2133 | ||
|
1
Apr 25
|
3212 | ||
|
1
Apr 25
|
4019 | ||
|
1
Apr 25
|
1520 | ||
|
4
Mar 25
|
6503 |