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

i have 2 fields, one we will pick a date, i would like the other to automatically compute -5 weeks from the selected date. i have played with the compute field in studio but have had no luck getting anything to work.

so far i have:


for record in self:

  record[("x_studio_field_CgBpj")] = record.x_studio_field_47ZJc - datetime.timedelta(weeks=+5)

Avatar
Discard
Best Answer

Odoo 11, Python 3.5

from datetime import datetime, timedelta
from odoo.tools.misc import DEFAULT_SERVER_DATETIME_FORMAT

class Test(models.Model):
_name = "dhac.rad.project"

test1 = fields.Datetime(string="Date Time A")
test2 = fields.Datetime(string="Date Time B", compute="dp_test1")

@api.depends('test1')
def dp_test1(self):
if self.test1:
raw = datetime.strptime(self.test1, DEFAULT_SERVER_DATETIME_FORMAT) - timedelta(weeks=5)
self.test2 = str(raw)
Avatar
Discard
Author

which parts of this will i need to change for my custom fields?

You should share the code you are currently editing.

Thanks

Author

i have, its in the original post.

im using the compute field in the studio.

from datetime import datetime, timedelta

from odoo.tools.misc import DEFAULT_SERVER_DATETIME_FORMAT

for record in self:

record[("x_studio_field_CgBpj")] = datetime.strptime(record.x_studio_field_47ZJc, DEFAULT_SERVER_DATETIME_FORMAT) - timedelta(weeks=5)

Try it bro, i don't test it

[tab]record[("x_studio_field_CgBpj")] = datetime.strptime(record.x_studio_field_47ZJc, DEFAULT_SERVER_DATETIME_FORMAT) - timedelta(weeks=5)

My code: pastebin.com/yYTL4E5h

Author

getting the error:

File "/odoo/odoo-server/odoo/tools/safe_eval.py", line 189, in assert_valid_codeobj

raise ValueError("forbidden opcode(s) in %r: %s" % (expr, ', '.join(opname[x] for x in codes)))

ValueError: forbidden opcode(s) in 'from datetime import datetime, timedelta\r\nfrom odoo.tools.misc import DEFAULT_SERVER_DATETIME_FORMAT\r\nfor record in self:\r\n record[("x_studio_field_CgBpj")] = datetime.strptime(record.x_studio_field_47ZJc, DEFAULT_SERVER_DATETIME_FORMAT) - timedelta(weeks=5)': IMPORT_NAME, IMPORT_FROM

Are both fields of the same type? (Datetime or Date). If not, you need to convert it. I tried something similar and this was the problem I had.

Related Posts Replies Views Activity
2
May 20
4502
1
Jan 17
4031
2
Jun 23
2263
2
Apr 23
21455
0
Oct 19
3160