Skip to Content
Menu
This question has been flagged
2 Replies
823 Views
Author Best Answer

Buenas, 

muchas gracias por ayudarme con la consulta. 

Implementando tu solución me sale el siguiente error:

forbidden opcode(s) in 'from datetime import datetime # Import datetime module\n\nfor record in records:\n record.x_studio_diagnostico_fecha = datetime.now() # Assign the current date and time': IMPORT_NAME, IMPORT_FROM, STORE_ATTR

Buscando el error en Copilot está relacionado con problemas de seguridad y me propone el siguiente código utilizando campos computados pero que me lleva al mismo error (forbidden opcode (s)). 

from odoo import models, fields, api
from datetime import datetime

class YourModel(models.Model):
    _inherit = 'your.model'

    x_studio_diagnostico_fecha = fields.Datetime(string="Diagnóstico Fecha", compute='_compute_diagnostico_fecha', store=True)

    @api.depends('some_field')
    def _compute_diagnostico_fecha(self):
        for record in self:
            record.x_studio_diagnostico_fecha = datetime.today()

Existe alguna forma de hacerlo? 

Un saludo!

Avatar
Discard
Best Answer

Your Python code for the server action appears to be mostly correct. However, I would suggest ensuring a couple of best practices to make it robust and prevent potential errors:

Here is the corrected Python code:

from datetime import datetime  # Import datetime module

for record in records:
    record.x_studio_diagnostico_fecha = datetime.now()  # Assign the current date and time

Steps to Create the Server Action

  1. Navigate to the Server Actions Menu:
    • Go to Settings > Technical > Automation > Server Actions.
    • Click Create to define a new server action.
  2. Set Up the Action:
    • Name: Provide a meaningful name (e.g., "Set Diagnostic Date").
    • Model: Select the model on which the action will run.
    • Action To Do: Select Execute Python Code.
    • Code: Paste the above Python script into the code editor.
  3. Trigger the Action:
    • Attach the server action to a trigger, such as:
      • A button in the form view.
      • An automated action (e.g., a scheduled job or a record change event).

Code Explanation

  • datetime.now():
    • This fetches the current date and time.
    • If you only need the date (without the time), use datetime.today().date().
  • x_studio_diagnostico_fecha:
    • Replace this with the actual technical name of your custom field where the date is stored.

Example Use Case

If this server action is attached to a button, clicking the button will update the x_studio_diagnostico_fecha field with the current date and time.

Let me know if you encounter issues while implementing this!

Avatar
Discard
Related Posts Replies Views Activity
1
Mar 25
407
0
Mar 25
2
1
Jan 25
455
0
Jul 24
686