Pular para o conteúdo
Menu
Esta pergunta foi sinalizada
1 Responder
4635 Visualizações


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
Cancelar
Melhor resposta

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
Cancelar
Publicações relacionadas Respostas Visualizações Atividade
1
jul. 24
2310
0
fev. 24
2345
1
abr. 20
2512
1
mar. 18
6327
2
out. 24
1185