Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
2145 Widoki

On Odoo16, I'm trying to create a cron job in code like this;

                self.env['ir.cron'].sudo().create({

                    'name': cron.cron_name,

                    'cron_name': cron.cron_name,

                    'active': False,

                    'interval_number': cron.interval_number,

                    'interval_type': cron.interval_type,

                    'numbercall': cron.numbercall,

                    'doall': cron.doall,

                    'model_id': cron.model_id,

                    'state': cron.state,

                    'code': cron.code.replace('{{store_id}}', str(self.id)),

                })

The cron job is created and I can see that the model_id is correct and referencing my model in ir.model. However, the cron job fails because "model_name" is NULL in ir_act_server. Looking at the model, the field is a stored related field as defined below;

model_name = fields.Char(related='model_id.model', string='Model Name', readonly=True, store=True)

If would seem that the related field is not being correctly set when a new record is created with model_id correctly set.

Most modules use XML to create cron jobs. I've checked and none of these attempt to explicitly set model_name, yet in the DB, model_name is correctly set for them.

If I manually edit the server action to change the model_id through the UI, the field is correctly updated.

Has anyone else seen this issue; whether specific to ir.action.server, or any related field not being set correctly?

Awatar
Odrzuć
Autor Najlepsza odpowiedź

Ok, got to the bottom of it. The value I was passing for the model_id was typed as char. This was fine for the DB as it implicitly converted it to an integer and the relationship to ir_model was correct, however the ORM didn't correctly relate this to ir.model at record creation. Changing the data type of the field cron.model_id to integer fixed the problem. Should it have been an integer in the first place? Yes. Should the related field have still worked? Yes; if the DB is happy with the relationship, then the ORM should be too.

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
0
kwi 22
3922
0
sty 17
4317
0
gru 15
4615
1
wrz 15
16880
3
wrz 23
11550