İçereği Atla
Menü
Bu soru işaretlendi
1 Cevapla
4393 Görünümler

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
Vazgeç
En İyi Yanıt

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
Vazgeç
Üretici

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

You should share the code you are currently editing.

Thanks

Üretici

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

Üretici

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.

İlgili Gönderiler Cevaplar Görünümler Aktivite
2
May 20
5980
1
Oca 17
5079
1
Şub 25
766
2
Haz 23
4068
2
Nis 23
23268