Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
1 Balas
4628 Tampilan


Hello, I have a date field and I need to extract its month and year using studio, I tried the following, but it gives me an error, how can I solve it? I thought that I needed to compute a new month field, but I got an error with this one:

for record in self:
  from datetime import datetime, date
  record[("x_studio_month")] = datetime.record.x_studio_from.strftime("%B")


The error is something like:

  ValueError: forbidden opcode(s) in 'for record in self:\n from datetime import datetime\n record[("x_studio_mes_desde")] = record.x_studio_desde.month': IMPORT_NAME, IMPORT_FROM

Avatar
Buang
Jawaban Terbai

Hi,

You can try the following method for the same:

1) Go to settings and turn on the developer mode and switch to the studio.
2) Now add 2 Text fields for Month and Year respectively.
3) Now define the compute methods, by clicking on the more option of the field. You can refer to Odoo Studio Book for any help: https://www.cybrosys.com/odoo/odoo-books/odoo-15-studio/build-an-app/define-compute-method/
4) In "Dependencies" add the technical name of the date field from which you need to extract the date.
5) Now in the "Compute" field of the month enter the following code "for rec in self:
rec['x_studio_month'] = rec.date_field.strftime('%B')"
6) Now in the "Compute" field of the year enter the following code "for rec in self:
rec['x_studio_year'] = rec.date_field.strftime('%Y')"
7) Now you can save it and close the studio view.

Now the fields will be computed when we enter the value into the date field

Regards

Avatar
Buang
Post Terkait Replies Tampilan Aktivitas
1
Jul 24
2289
0
Feb 24
2331
1
Apr 20
2501
1
Mar 18
6317
2
Okt 24
1179