Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
4379 Widoki

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)

Awatar
Odrzuć
Najlepsza odpowiedź

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)
Awatar
Odrzuć
Autor

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

You should share the code you are currently editing.

Thanks

Autor

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

Autor

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.

Powiązane posty Odpowiedzi Widoki Czynność
2
maj 20
5958
1
sty 17
5070
1
lut 25
753
2
cze 23
4055
2
kwi 23
23242