Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
1 Odpovědět
4463 Zobrazení

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
Zrušit
Nejlepší odpověď

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
Zrušit
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.

Related Posts Odpovědi Zobrazení Aktivita
2
kvě 20
6046
1
led 17
5154
1
úno 25
779
2
čvn 23
4144
2
dub 23
23357