Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
1397 Lượt xem

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

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!

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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!

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 3 25
658
0
thg 5 25
255
1
thg 1 25
751
0
thg 7 24
839