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

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?

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

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.

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 4 22
4079
0
thg 1 17
4404
0
thg 12 15
4704
1
thg 9 15
16973
3
thg 9 23
11768